学习目标:
①:Python字符串支持的操作
- Python日常
内容展示:
①:Python字符串支持的操作
# 1.通过索引获取字符串中的某个字符
s = 'hello world'
print(s[0]) # h
print(s[1]) # e
print(s[2]) # l
print(s[3]) # l
# 2.分片
print(s[:2]) # [0,2) he
print(s[::2]) # step=2 hlowrd
print(s[::-1]) # 倒序 dlrow olleh
s2 = 'xyz'
print(10 * s2) # xyzxyzxyzxyzxyzxyzxyzxyzxyzxyz
print('b' in s2) # False
print('x' in s2) # True
print('b' not in s2) # True
print(len(s2)) # 3
print(min(s2)) # x
print(max(s2)) # z
a = [0,2,5,9,6]
print(2 not in a) # False
print(min(a)) # 0
print(max(a)) # 9
print(len(a)) # 5
a[0] = 20
print(a) # [20, 2, 5, 9, 6]
总结:
1.字符串的分片
[x:y] ———> [x,y)
[::2] ———>step=2
[::-1] ———>倒序
[:2] ———>[0,2)
[2:] ———>2,最后]
2.字符串的乘法
10 * str
3.判断字符在字符串中是否存在
‘b’ in s1
4.字符串的长度
len(s1)
5.字符串中按ASCII值算最大字符和最小字符
max(s1)
min(s1)
- CSDN Python日常 第【3】3 篇
- [x] 下一篇链接