3.3字符串的公共功能
1.相加:字符串 + 字符串
v1 = "吉林省" + "长春市"
print(v1)
2.相乘:字符串 * 整数
data = "alex" * 3
print(data)
3.长度
data = "吉林省长春市"
value = len(data)
print(value)
4.获取字符串中的字符,索引
message = "吉林省长春市"
# 0 1 2 3 4 5
# .....-3-2-1
print(message[0])#"吉"
print(message[1])#"林"
print(message[2])#"省"
print(message[-1])#"市"
print(message[-2])#"春"
print(message[-3])#"长"
注意;字符串能通过索引取值,不能修改值。
message = "吉林省长春市"
index = 0
while index < len(message):
value = message(index)
print(value)
index += 1
message = "吉林省长春市"
index = len(message) - 1
while index >=0:
value = message(index)
print(value)
index -= 1
5.获取字符串中的子序列,切片
message = "吉林省长春市"
print(message[0:2])#"吉林"
print(message[3:6])#"长春市"
print(message[3:])#"长春市"
print(message[:5])#"吉林省长春"
print(message[4:-1])#"长春"
message = "吉林省长春市"
value = message[:3] + 加 + message[5:]
print(value)
6.步长,跳着读字符串的内容
name = "生活不是电影,生活比电影苦"
print(name[0:5:2])#生不电[前两表示区间范围,最后一个表示步长]
print(name[:8:2])#生不电,[区间的范围前边不写表示从零开始]
print(name[2::3])#不影活影[区间的范围后边不写表示到最后]
print(name[::2])#生不电,活电苦[区间范围不写表示整个字符串]
print(name[8:1:-1])#活生,影电是不[倒序]
7.循环
while循环
-
message = "吉林省长春市" index = 0 while index < len(message): value = message(index) print(value) index += 1
for循环
-
message = "吉林省长春市" for char in message: print(char)
-
rang,帮我们创建一系列的数字
rang(10) #[0,1,2,3,4,5,6,7,8,9] rang(1,10) #[1,2,3,4,5,6,7,8,9] rang(1,10,2) #[1,3,5,7,9] rang(10,1,-1) #[10,9,8,7,6,5,4,3,2]
-
For range
for i in range(10): print(i)
message = "吉林省榆树市" for i in range(len(message)): print(message[i])
一般应用场景:
- while,一般在做无限制(未知)循环时使用
- for,循环一般在已知循环数量时使用
3.4转换
num = 999
data = str(num)
print(data) #"999"
3.5其他
3.5.1字符串不可修改
name = "吴佩琦"
name[1]
name[1:2]
列表可以修改
num_list = {11,22,33}
num_list[0]
num_list[0] = 666
标签:index,name,python11,长春市,value,print,message
From: https://www.cnblogs.com/zhangyj213/p/17744727.html