= += -= *= /= //= %= **=
< > = <= == !=
in not in
and or not
0 and 9 and 8 and 5 #0,有一假取假
4 or 0 or 9 or 8 #4,有一真取真
+ - / // ** %
布尔值转换数字的时候非0都是True 0是False
print(bool(0)) #False
print(bool(-9)) #True
布尔值转换字符串的时候不是空格就是True,空格是False
print(bool("你好")) #True
print(bool(" ")) #False
print(int(True)) #1
print(bool(False)) #0
print(str(True)) #字符串类型的True
print(str(False)) #字符串类型的False
从左往右数 0——n
从右往作数 -1-—— -字符串的长度
下标查找如果过界,就报错
a="你好你好"
print(a[2]) #你
print(a[-3]) #好
msg[3:5] 取前不取后
msg = "今天是个好日子!"
print(msg[3:5]) #个好
msg[-5:-2:1] -25起始位置 -2终止位置 1步长 默认不写是1
1表示从左往右 -1表示从右往左
msg="今天是个好日子!"
print(msg[-5:-2]) #个好日
msg = "今天是个好日子!下雨了!"
print(msg[2:6:2]) #是好
msg="今天是个好日子!下雨了!"
print(msg[6:1:-1]) #子日好个是
切片如果过界,就取到最后一个内容
首字母大写
s = "zhyyz"
s1=s.capitalize()
print(s1) #Zhyyz
全部大写
s = "zhyyz"
s1=s.upper()
print(s1) #ZHYYZ
全部小写
s = "ZhYYz"
s1=s.lower()
print(s1) #zhyyz
返回的数量
以什么结尾
以什么结尾
通过元素查找下标 查找没有的返回-1
通过元素查找下标 查找没有的就报错
s = "zhyyz"
print(s.count("y")) #2
print(s.endswith("z")) #True
print(s.startswith("y")) #False
print(s.find("y")) #2
字符串格式化
s= "zhy"
s1="zhy{0},{2},{1}"
s2="zhy{a},{b},{c}"
print(s.format("你好","yz","少年")) #zhy
print(s1.format("你好","yz","少年")) #zhy你好,少年,yz
print(s2.format(a="你好",b="yz",c="少年")) #zhy你好,yz,少年
插入字符
s= "zhyyzily"
#print(s.join("")) #错误
print("".join(s)) #z_h_y_y_z_i_l_y
分割
s = "zhyyzilzy"
print(s.print("z")) #['', 'hyy', 'il', 'y']
脱 脱掉头尾两边的空格 换行符\n
s = " zhyyzily "
print(s.strip()) #zhyyzily
脱掉指定字符 如果有空格 脱掉空格
s = " zhyyzily "
print(s.strip(" y")) #zhyyzil
替换 第一个放要被替换的第二个是替换的内容
s = " zhyyzily "
print(s.replace("z","y")) # yhyyyily
大小写转换 大写转换成小写 小写转换成大写
特殊符号分割的每个单词首字母大写
s = " zhy yz il y "
print(s.title()) # Zhy Yz Il Y
判断是不是纯数字的
盘对是不是汉字和字母
s = "你好啊zhy撒大声地"
print(s.isalpha()) #True
s="你好嗨啊" #你
s_len= len(s) #好
count=0 #嗨
while count < s_len: #啊
print(s[count])
count += 1
for关键字 i变量 in关键字 要迭代的对象 冒号:
for循环结构体
s="你好嗨啊"
for n in s:
print(n)
手机扫一扫
移动阅读更方便
你可能感兴趣的文章