字符串公共功能
-
相加:字符串 + 字符串 v1 = "tencent" + "云yuansheng"
print(v1)
-
相乘:字符串 * 整数 data = "腾讯" * 3
print(data)
-
长度 data = "云原生大数据"
value = len(data)
print(value)
-
获取字符串中的字符,索引 message = "来腾讯课堂学习k8s容器云"
print(message[0])
print(message[1])
print(message[2])
print(message[-1])
print(message[-2])
print(message[-3])
注意:字符串中是能通过索引取值,无法修改值。【字符串在内部存储时不允许对内部元素修改,想修改只能重新创建。】 message = "来腾讯课堂学习k8s容器云"
index = 0
while index < len(message):
value = message[index]
print(value)
index += 1
message = "来腾讯课堂学习k8s容器云"
index = len(message) - 1
while index >=0:
value = message[index]
print(value)
index -= 1
-
获取字符串中的子序列,切片 message = "云原生k8s容器云架构"
print(message[0:2])
print(message[3:7])
print(message[3:])
print(message[:5])
print(message[4:-1])
print(message[4:-2])
print(message[4:len(message)])
注意:字符串中的切片只能读取数据,无法修改数据。【字符串在内部存储时不允许对内部元素修改,想要修改只能重新创建】 data = "云原生大数据"
value = data[:3] + "Python" + data[5:]
print(value)
-
循环
-
while循环 message = "云原生k8s容器云架构"
index = 0
while index < len(message):
value = message[index]
print(value)
index += 1
云
原
生
k
8
s
容
器
云
架
构
-
for循环 message = "云原生k8s容器云架构"
for char in message:
print(char)
-
range,帮助我们创建一系列的数字 range(10)
range(1,10)
range(1,10,2)
range(10,1,-1)
-
For + range for i in range(10):
print(i)
message = "云原生k8s容器云架构"
for i in range(5):
print(message[i])
云
原
生
k
8
message = "云原生k8s容器云架构"
for i in range(len(message)):
print(message[i])
一般应用场景:
message = "云原生架构"
for char in message:
if char == "架":
continue
print(char)
云
原
生
构
转换
num = 999
data = str(num)
print(data)
data_list = ["alex","eric",999]
data = str(data_list)
print(data)
一般情况下,只有整型转字符串才有意义。
其他
1 字符串不可被修改
name = "腾讯"
name[1]
name[1:2]
num_list = [11,22,33]
num_list[0]
num_list[0] = 666
2 代码示例
"""
@Author: yinwu
@Time: 2021年07月29日 16:44
@FileName: 公共功能.py
"""
"""
v1 = 675
print(bin(v1))
v2 = "0b11000101"
print(int(v2, base=2))
v3 = "11000101"
print(int(v3, base=2))
"""
"""
v1 = 123
v11 = bin(v1)
v2 = 456
v21 = bin(v2)
data = v11[2:] + v21[2:]
result = int(data, base=2)
print(result)
"""
"""
v1 = 123
v2 = 456
data = bin(v1)[2:] + bin(v2)[2:]
result = int(data, base=2)
print(result)
"""
"""
v1 = 123
v2 = 456
data = bin(v1)[2:].zfill(16) + bin(v2)[2:].zfill(16)
result = int(data,base=2)
print(result)
"""
"""
空、0转换布尔值都是False
"""
"""
456
666
345
"""
"""
text = input("请输入内容:")
text = text.replace("云原生", "***")
text = text.replace("大数据", "***")
print(text)
"""
"""
name = "aleX leNb "
print(name.strip())
print(name.startswith("al"))
print(name[0:2] == "al")
print(name.endswith("Nb"))
print(name[-2:] == "Nb")
v1 = name.replace("l", "p")
print(v1)
v2 = name.split("l")
print(v2)
v3 = name.split("l", 1)
print(v3)
print(name.upper())
print(name.lower())
"""
"""
name = "云原生"
data = name[::-1]
print(data)
"""
"""
s = "123a4b5c"
print(s[0:3])
print(s[3:6])
print(s[-1])
print(s[len(s) - 1])
print(s[-3:0:-2])
"""
"""
message = "伤情最是晚凉天,憔悴厮人不堪言"
index = 0
while index < len(message):
print(message[index])
index += 1
"""
"""
message = "伤情最是晚凉天,憔悴厮人不堪言"
for item in message:
print(item)
"""
"""
message = "伤情最是晚凉天,憔悴厮人不堪言"
for index in range(len(message)):
print( message[index] )
"""
"""
message = "伤情最是晚凉天,憔悴厮人不堪言"
for index in range(len(message)-1,-1,-1):
print(message[index])
"""
"""
for num in range(3, 0, -1):
text = "倒计时{}秒".format(num)
print(text)
"""
"""
text = input("请输入一段文本:")
count = 0
for item in text:
if item == "浪":
count += 1
print(count)
"""
"""
num1 = input("请输入:")
num1_list = []
for item in num1:
if item.isdecimal():
num1_list.append(item)
data1 = "".join(num1_list)
num2 = input("请输入:")
num2_list = []
for item in num2:
if item.isdecimal():
num2_list.append(item)
data2 = "".join(num2_list)
result = int(data1) + int(data2)
print(result)
"""
"""
num1 = input("请输入:")
data1 = ""
for item in num1:
if item.isdecimal():
data1 += item
num2 = input("请输入:")
data2 = ""
for item in num2:
if item.isdecimal():
data2 += item
result = int(data1) + int(data2)
print(result)
"""
|