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]