python字符串的遍历和切片
下标(索引),在计算机中,从0开始
1.遍历
字符串是可迭代对象
while遍历
msg = 'hello world'
i = 0
while i < len(msg):
print(msg[i])
i += 1
for遍历
msg = 'hello world'
for x in msg:
print(x)
2.切片
切片:截取对象的一部分的操作。
字符串、列表、元组支持切片,因为都有下标
中间用冒号(:)隔开
不包含结束
步长表示选取的间隔,默认为1
hello world的下标