1、运算符
结果是值
算数运算
a = 10 * 10
赋值运算
a = a + 1 a+=1
结果是布尔值
比较运算
a = 1 > 5
逻辑运算
a = 1>6 or 1==1
成员运算
a = "蚊" in "郑建文"
2、基本数据类型
1)数字 int ,
将字符串转换为数字 int()
当前数字的二进制,至少用n位表示 r = age.bit_length()
2)字符串 str
首字母大写 str.capitalize()
设置宽度,并将内容居中test.center(20,"中")
字符串中寻找,寻找子序列的出现次数test.count('ex')
以什么结尾 test.endswith('ex')
以什么开始 test.startswith('ex')
从开始往后找,找到第一个之后,获取其未知,未找到 -1 test.find('ex'); index找不到,报错 test.index('8')
格式化,将一个字符串中的占位符替换为指定的值
# test = 'i am {name}, age {a}' # v = test.format(name='alex',a=19)
test = 'i am {0}, age {1}' # v = test.format('alex',19)
test.format_map({"name": 'alex', "a": 19})
字符串中是否只包含 字母和数字 v = test.isalnum()
字符串是否是字母汉字 v = test.isalpha()
当前输入是否是数字 v1 = test.isdecimal v2 = test.isdigit() v3 = test.isnumeric()
将字符串中的每一个元素按照指定分隔符进行拼接 v = "_".join(test)
判断是否全部是大小写 和 转换为大小写 v1 = test.islower() v2 = test.lower()
v1 = test.isupper() v2 = test.upper()
移除指定字符串,有限最多匹配 v = test.lstrip() v = test.rstrip() v = test.strip()
对应关系替换 m = str.maketrans("aeiou", "12345") new_v = v.translate(m)
分割v = test.partition(' ') v = test.rpartition('s')
v = test.split('s',2) v = test.rsplit()
按行分隔 v = test.splitlines()
大小写转换 test.swapcase()
将指定字符串替换为指定字符串 v = test.replace("ex",'bbb')
因为可以迭代,所以可以运行:1) for循环 2)索引,下标,获取字符串中的某一个字符 test[]
3)切片test[0:-1]包头不包尾 4)获取长度 len(test)