数字 int # 整型 所有的功能,都在int里 a = 123 v = a.bit_length() # 软件自带函数联想 print(v) - int 将字符串转换成数字 a = '123' print(type(a),a) # type 显示为类型 b = int(a) # 把字符串转换成数字 b = b + 1000 # 转换成数字后可运算 print(type(b),b) # 即显示类型,又显示值 - bit_length(self) # 所有self都可以不输入 当前数字的二进制数值的位数 # 1 1 # 2 10 # 3 11 # 4 100 # 5 101 # 当前数字的二进制至少用几位来表示 r = age.bit_length() 字符串 str - def capitalize(self) 首字母变大写 a = 'alex' v = a.capitalize() print(v) # 返回结果 Alex - def center(self, int, fillchar: unicode = ...) 在总长度为windth的范围内,居中显示self,fillchar空白处显示内容,需引用'' a = 'alex' v = a.center(10,'*') print(v) #返回结果 ***alex*** - def count(self, x: unicode) 在字符串中,对于某个x出现的次数 a = 'alexalex' v = a.count('ex') print(v) # 返回结果 2 - def find(self, sub: unicode, start: int = ..., end: int = ...) 在字符串中,在【x,y】的区间内,第一次找到v的位置,如果找不到,则反馈-1 # 字符串位置顺序从0开始 a = 'alex' v = a.find('l',3,4) print(v) # 返回结果 -1 v = a.find('l',0,4) # 返回结果 1 - def format(self, *args: object, **kwargs: object) 在字符串中,针对某部分进行格式化替换{},{} 1、按照默认顺序,不指定特定位置 a = '{} {}' v = a.format('Hallo','World') print(v) # 或者直接print print("{} {}".format("hello","world") ) # 返回结果 hello world 2、设置指定位置,可以多次使用 a = '{0} {1} {0}' v = a.format('Hallo','or') print(v) # 或者直接print print('{0} {1} {0}'.format('Hello','or')) # 返回结果 Hello or Hallo 3、使用列表格式化 person = {'name':'opcai','age':20} print('My name is {name} . I am {age} years old .'.format(**person)) # 返回结果 My name is opcai . I am 20 years old . 4、通过列表格式化 stu = ['opcai','linux','MySQL','Python'] print('My name is {0[0]} , I love {0[1]} !'.format(stu)) # 返回结果 My name is opcai , I love linux ! - def expandtabs(self, tabsize: int = ...) 通过魔法,制作格式化文本或表格 # 另起一行 # 制表符 # 和之前的加在一起等于输入的数字int s = 'username email password alex alex@qq.com 123 alex alex@qq.com 123' v = s.expandtabs(20) print(v) # 返回结果 username email password alex alex@qq.com 123 alex alex@qq.com 123 # username到email之间,正好是20个格子 - def index(self, sub: unicode, start: int = ..., end: int = ...) 在字符串中,在【x,y】的区间内,找到子序列的位置,如果找不到,则反馈error 和 find 类似 a = 'alex' v = a.index('l',3,4) print(v) # 返回结果 ValueError: substring not found - def isalnum(self) 字符串中,是否包含 字母或数字 text = '123abcd' v = text.isalnum() print(v) # 返回结果 True - def isalpha(self) 字符串中,是否全是字母 text = '123abcd' v = text.isalpha() print(v) # 返回结果 False - def isdecimal(self) 十进制数字 当前输入是否是数字 十进制数字 - def isdigit(self) 包含数字形式 当前输入是否是数字 任何数字形式,是否包含十进制数字 - def isnumeric(self) 形式全包含 当前输入是否是数字 任何形式,包括中文(python3) - def isdecimal isdigit isnumeric 三者的区别 test = '2' v1 = test.isdecimal() v2 = test.isdigit() v3 = test.isnumeric() print (v1,v2,v3) # 返回结果 True True True test = '②' v1 = test.isdecimal() v2 = test.isdigit() v3 = test.isnumeric() print (v1,v2,v3) # 返回结果 False True True test = '二' v1 = test.isdecimal() v2 = test.isdigit() v3 = test.isnumeric() print (v1,v2,v3) # 返回结果 False False True - def isidentifier(self) 按照字母,数字,下划线顺序进行鉴别 a = 'adb123_' v = a.isidentifier() print(v) # 返回结果 True - def islower(self) 序列中是否全部是小写,只要有一个大写 就false a = 'Abcdefg' v = a.islower() print(v) # 返回结果 False - def isprintable(self) 打印的时候都能看到的值就True,如果存在不可显示的,比如 、 之类,则False - def isspace(self) 判断序列里是否全部都是空格,只要有一个数字或文本,则False # 空字符串不含空格,False - def istitle(self) 判断字符串是否全是以大写为开头的标题 test = 'Tuple[unicode, unicode, unicode]' v1 = test.istitle() print(v1) # 返回结果 Fasle v2 = test.title() print(v2) # 返回结果 Tuple[Unicode, Unicode, Unicode] v3 = v2.istitle() print(v3) #返回结果 True - def isupper(self) 判断字符串中所有的字符都是大写,只要有一个小写,就是False test = 'Tuple[unicode, unicode, unicode]' v1 = test.isupper() print(v1) # 返回结果 Fasle v2 = test.upper() print(v2) # 返回结果 TUPLE[UNICODE, UNICODE, UNICODE] v3 = v2.isupper() print(v3) #返回结果 True - def join(self, iterable: Iterable[unicode]) 在字符串中插入self,注意是把self插入引用,每一个字符之间 test = '你是风儿我是沙' print(test) t = ' ' v = t.join(test) # 在序列test中插入t print(v) #返回结果 你 是 风 儿 我 是 沙 or print(' '.join('你是风儿我是沙')) #返回结果 你 是 风 儿 我 是 沙 - def ljust(self, int, fillchar: unicode = ...) 在引用的self右边,按照输入宽度要求,填充某标记 test = 'alex' v = test.ljust(20,'*') print(v) #返回结果 alex**************** - def rjust(self, int, fillchar: unicode = ...) 在引用的self左边,按照输入宽度要求,填充某标记 test = 'alex' v = test.rjust(20,'*') print(v) #返回结果 ****************alex - def lower(self) 把字符串中所有的字符变成小写 # 验证码输入大小写都可以,这个时候后台执行lower,后台显示都是小写 - def strip(self, chars: unicode = ...) 在字符串中去除self ()默认为空白 移除指定字符串 有限最多匹配 test = 'xalex' v1 = test.strip('x') v2 = test.lstrip('x') v3 = test.rstrip('x') print(v1,v2,v3) #返回结果 ale alex xale - def lstrip(self, chars: unicode = ...) 在字符串中左边去除self - def rstrip(self, chars: unicode = ...) 在字符串中右边去除self - def maketrans(self, *args, **kwargs) 对于两组字符串进行对应顺序的替换 结合 def translate 一起用 v = '我爱你,你爱他,他爱她,她爱他' test = '你我他' test1 = '123' m = maketrans(test,test1) new_v = v.translate(m) print(new_v) #返回结果 2爱1,1爱3,3爱她,她爱3 - def translate(self, table: Union[Dict[int, Any], unicode]) 把对应替换的关系,重新应用到str语句中 - def partition(self, sep: unicode) 在字符串中,以输入的x为切割符,从左到右找到第一个x并进行分割 显示x Bug:只能分割成三段,且x在中间 test = 'testabdeghrsoelt' v1 = test.partition('s') print(v1) v2 = test.rpartition('s') print(v2) #返回结果 ('te', 's', 'tabdeghrsoelt') #返回结果 ('testabdeghr', 's', 'oelt') - def rpartition(self, sep: unicode) -> Tuple[unicode, unicode, unicode]: ... 在字符串中,以输入的x为切割符,从右到左找到第一个x并进行分割 Bug:只能分割成三段 - def split 在字符串中,以输入的x为切割符,从左到右找到x并进行分割 不显示x 可以根据x分成 n+1 段,n可以进行设置,不输入则为全部查找分割 test = 'testabdesghrsoelt' v1 = test.split('s') v2 = test.split('s',1) v3 = test.rsplit('s',1) print(v1,v2,v3) #返回结果 ['te', 'tabde', 'ghr', 'oelt'] #返回结果 ['te', 'tabdesghrsoelt'] #返回结果 ['testabdesghr', 'oelt'] - def rsplit(self, sep: Optional[unicode] = ..., maxsplit: int = ...) 在字符串中,以输入的x为切割符,从右到座找到x并进行分割 不显示x 可以根据x分成 n+1 段,n可以进行设置,不输入则为全部查找分割 - def splitlines(self, keepends: bool = ...) 分割,只能根据 换行符进行分割,true。flase :是否保留换行符 test = '124 40869994 8282292 345' v = test.splitlines(True) print(v) v = test.splitlines() print(v) # 返回结果 # ['124 ', '40869994 ', '8282292 ', '345'] # ['124', '40869994', '8282292', '345'] - def startswith(self, prefix: Union[unicode, Tuple[unicode, ...]], start: int = ..., end: int = ...) 判断是否以XXX开始 test = 'backeit 1.1 1.1' v = test.startswith('ba') print(v) # 返回结果 True def endswith 判断是否以XXX结尾 - def swapcase(self) 字符串中所有大小写进行转换 all test = 'backeitFJEKEI' v = test.swapcase() print(v) # 返回结果 BACKEITfjekei - def zfill(self, int) 设置新字符串长度,引用原字符串右对齐,前面填充0 test = 'backeitFJEKEI' v = test.zfill(20) print(v) # 返回结果 0000000backeitFJEKEI - def replace(self, old: unicode, new: unicode, count: int = ...) 替换 test = '你是风儿我是沙' v = test.replace('你','他') print(v) # 返回结果 他是风儿我是沙 - def decode(self, encoding: unicode = ..., errors: unicode = ...) -> unicode: ... - def encode(self, encoding: unicode = ..., errors: unicode = ...) -> str: ... - 7个基本魔法 join split find strip upper lower replace - 4个灰魔法 [int] 索引,获取字符串中某一个字符 [int1:int2] 索引切片,获取两个区间之间的字符,>=int1,<int2 len() 检测字符串的字符个数 or 列表中的元素个数 for 循环 变量名 in 字符串: 循环接收器 # 案例 test = '你是风儿我是沙' index = 0 while index < len(test): v = test[index] print(v) index += 1 print('====end====') # 返回结果 你 是 风 儿 我 是 沙 ====end==== # 案例另一种表达方法 test = '你是风儿我是沙' for A in test: print(A) - 1个深灰魔法 字符串一旦创建,不可修改 一旦修改或者拼接,都会造成重新生成字符