字符串(一)
字符串索引(查找)
name='my name is zhangsan!'
char=name[0]
print(char)
char1=name[-1]
print(char1)
结果:
字符串切片
格式是:格式:[起始索引:结束索引:[步长]]
s='hello world'
print(1,s[0:8:1])
print(2,s[0:8:2])
print(3,s[0:9])
print(4,s[0:])
print(5,s[:8])
print(6,s[:])
print(7,s[::2])
print(8,s[:8:2])
print(9,s[4::2])
print(10,s[0:8:-1])
print(11,s[::-1])
print(12,s[-8:5])
print(13,s[0:-8])
print(14,s[-1:-8:-2])
这里的(1, ) ,(2,)…(14,)只是单纯的为了分辨
遍历字符串与字符串的拼接
#遍历字符串
for s in 'tom':
print(s)
else:
print('程序执行完成')
#字符串拼接
na