not in
name = "pangzhonglei"
if "zhong" in name:
print("yes")
else:
print("no")
if "zhong" not in name:
print("yes")
else:
print("no")
输出结果:yes no
基本数据类型: 整型 字符串 列表 元组 字典 布尔
整型的基本操作
1.将字符串以16进制的形式转换
num = "b"
v = int(num, base=16)
print(v)
输出结果:11
2. bit_length() 当前数字的二进制至少用几位表示
age = 5
r = age.bit_length()
print(r)
输出结果:3
字符串的基本操作
1. capitalize() 将首字母大写
test = "alex"
v = test.capitalize()
print(v)
输出结果:Alex
2. casefold() 将字母变成小写
test1 = "aLex"
v1 = test1.casefold()
print(v1)
输出结果:alex
3. center() 设置宽度并将内容居中
ljust() 设置宽度并将内容放在左面
rjust() 设置宽度并将内容放在右面
tes2 = "alex"
v2 = tes2.center(20)
v22 = tes2.center(20, "*")
print(v2)
print(v22)
输出结果:
alex
********alex********
alex****************
****************alex
4. count(self, sub, start=None, end=None) 从字符串中寻找子序列出现的次数
test3 = "alexalex"
v3 = test3.count("x")
print(v3)
输出结果:2
5. startswith() 以什么开始
endswidth() 以什么结束
test4 = "alex"
v4 = test4.startswith("a")
v44 = test4.endswith("e")
print(v4)
print(v44)
输出结果:True False
6. find() 从开始往后找,找到第一个后,获取其位置,如果返回的是-1 表示找不到
test5 = "alexalex"
v5 = test5.find("ex")
print(v5)
输出结果:2
7. format() 格式化,将一个字符串中的占位符替换为指定的值
test6 = "I am {name}"
v6 = test6.format(name="alex")
print(v6)
输出结果:I am alex, age: 20 I am alex, age: 20
8. format_map() 格式化,传入的值是个字典
test7 = "I am {name}, age: {age}"
v7 = test7.format_map({"name": "alex", "age": 20})
print(v7)
输出结果:I am alex, age: 20
9. isalnum() 判断是否是字母或数字
test8 = "sfa2323"
test88 = "sfa2323-="
v8 = test8.isalnum()
v88 = test88.isalnum()
print(v8)
print(v88)
输出结果:True False
10. expandtabs() 把字符串中的 tab 符号(" ")转为空格,tab 符号(" ")默认的空格数是 8
test9 = "12345678 9"
v9 = test9.expandtabs(6)
print(v9)
str = "this is string example....wow!!!"
print ("Original string: " + str)
print ("Defualt exapanded tab: " + str.expandtabs())
print ("Double exapanded tab: " + str.expandtabs(16))
test99 = "username email passwd linlei 345@qq.com 123456 linlei 345@qq.com 123456 linlei 345@qq.com 123456 "
v99 = test99.expandtabs()
print(v99)
输出结果:
12345678 9
Original string: this is string example....wow!!!
Defualt exapanded tab: this is string example....wow!!!
Double exapanded tab: this is string example....wow!!!
username email passwd
linlei 345@qq.com 123456
linlei 345@qq.com 123456
linlei 345@qq.com 123456
11. isalpha() 判断是否是字母
t = "sfa2323"
s = t.isalpha()
print(s)
输出结果:False
12. isdecimal() isdigit() 判断是否是数字
t1 = "二"
s1 = t1.isdecimal()
s11 = t1.isdigit()
s111 = t1.isnumeric()
print(s1, s11, s111)
输出结果:False False True
13. swapcase() 大小写字母转换
t3 = "alEx"
s3 = t3.swapcase()
print(s3)
输出结果:ALeX
14. 判断是否是标识符 (字母,数字,下划线)
t4 = "_99"
s4 = t4.isidentifier()
print(s4)
输出结果:True
15. isprintable() 是否存在不可显示的字符
t4 = "sfsa sfasdf"
s4 = t4.isprintable()
print(s4)
输出结果:False
16. isspace() 判断是否全部为空格
t5 = "jafs asfj"
s5 = t5.isspace()
print(s5)
输出结果:False
17. title() 将一句话的首字母大写
t6 = "i am pang "
s6 = t6.title()
print(s6)
输出结果:I Am Pang
18. istitle() 判断一句话首字母是否是大写
t7 = "I Am Pang"
s7 = t7.istitle()
print(s7)
输出结果:True
19. join() 将字符串中每一个元素按照指定分隔符进行拼接
t8 = "我将带头冲锋"
s8 = "_".join(t8)
print(s8)
输出结果:我_将_带_头_冲_锋
20. lower() 将字母全部转换为小写字母
islower() 判断字母是否全部为小写字母
upper() 将字母全部转换为大写字母
isupper() 判读字母是否全部为大写字母
t9 = "isDDfs"
s9 = t9.lower()
print(s9)
t0 = "issd"
s0 = t0.islower()
print(s0)
输出结果:isddfs True
21. strip() 去除字符串两边的空格、/n、/t、指定的字符
lstrip() 去除字符串左边的空格、/n、/t、指定的字符
rstrip() 去除字符串右边的空格、/n、/t、指定的字符
a = "nalexm"
b = a.lstrip("n")
c = a.strip("nm")
print(b)
print(c)
输出结果:alexm alex
22. 按照一个匹配规则进行替换
a1 = "afldsifwlfposaausf"
b1 = str.maketrans("aeiou", "12345")
c1 = a1.translate(b1)
print(c1)
输出结果:1flds3fwlfp4s115sf
23. partition() 字符串分割,只分割一次
split() 字符串分割,分割多次,次数可以指定
a2 = "kasfsaasijswskfji"
b2 = a2.partition("s")
print(b2)
b22 = a2.rpartition("s")
print(b22)
c2 = a2.split("s")
print(c2)
cc2 = a2.split("s", 2)
print(cc2)
c22 = a2.rsplit("s")
print(c22)
输出结果:
("ka", "s", "fsaasijswskfji")
("kasfsaasijsw", "s", "kfji")
["ka", "f", "aa", "ij", "w", "kfji"]
["ka", "f", "aasijswskfji"]
["ka", "f", "aa", "ij", "w", "kfji"]