##将首字母改成大写# name = ‘alex‘# v = name.capitalize()# print(v) ##将首字母改成小写# name = ‘Alex‘# v = name.casefold()# print(v) ###传一个值,两边补的是添加的字符# name = ‘Alex‘# v = name.center(20,‘*‘)# print(v) ##统计字符串出现的次数# name = ‘Alex‘# v = name.count(‘A‘)# print(v) ##判断字符的结尾是否以某个字符串结尾的# name = ‘Alex‘# v = name.endswith(‘ex‘)# print(v) ##制表符(包含前边的值,整体占8个)# name = ‘Alex\tegon\twupeiqi‘# v = name.expandtabs(8)# print(v) ##索引# name = ‘Alex‘# v = name.find(‘A‘)# print(v) ##字符串格式化# tpl = ‘我是:{0};年龄:{1};性别:{2}‘# v = tpl.format(‘李杰‘,‘12‘,‘女‘)# print(v) # tpl = ‘我是:{name};年龄:{age};性别:{sex}‘# v = tpl.format(name = ‘李杰‘,age = 12,sex = ‘女‘)# print(v) ##根据键找到对应位置的值# tpl = ‘我是:{name};年龄:{age};性别:{sex}‘# v = tpl.format_map({‘name‘:‘李杰‘,‘age‘:‘12‘,‘sex‘:‘女‘})# print(v) ##找到对应字符串的索引# name = ‘Alex‘# v = name.index(‘A‘)# print(v) #判断# name = ‘Alex‘# v = name.isalnum()#只能包含字母、数字、汉字# print(v) ##判断是否全部为字母# name = ‘Alex‘# v = name.isalpha()# print(v) ##判断是否全部为阿拉伯数字# my_num = ‘123‘# v = my_num.isdecimal()# print(v) ##能识别阿拉伯数字②# my_num = ‘123②‘# v = my_num.isdigit()# print(v) ##能识别多种字符# my_num = ‘123②二‘# v = my_num.isnumeric()# print(v) ##判断是否为关键字# my_num = ‘num‘# v = my_num.isidentifier()# print(v) ##判断是否都为小写# name = ‘alex‘# v = name.islower()# print(v) ##在字符串中出现的打印不出来的为False# name = ‘al\nex‘# v = name.isprintable()# print(v) ##判断是否全部为空格# name = ‘al ex‘# v = name .isspace()# print(v) ##判断是否全部为大写# name = ‘alex‘# v = name.isupper()# print(v) #内部循环每一个元素# name = ‘alex‘# v = ‘*‘.join(name)# print(v) ##把值放到左边,右边填充相应剩下长度的字符# name = ‘alex‘# v = name.ljust(20,‘*‘)# print(v) ##不仅可以移除空白,也可以移除换行符,制表符也可以自定义# name = ‘alex‘# v = name.strip(‘x‘)# print(v) ##将字符串的大小写互换# name = ‘Alex‘# v = name.swapcase()# print(v) ##替换字符串# name = ‘你love他‘# v = name.replace(‘love‘,‘爱‘)# print(v) ##分割,将分割后的元素剔除# name = ‘你love他‘# v = name.split(‘love‘)# print(v)
时间: 2024-10-27 01:34:12