test = 'alEx' print(test.capitalize()) # 首字母大写 其他都变小写 print(test.casefold()) # 变小写 更牛逼 print(test.lower()) # 全部变小写 print(test.center(20, '*')) # 设置宽度,一共20个位置,将test放中间,其他用*拼接 print(test.count('E', 1, 2)) # test中存在E的数量,从哪开始到哪结束,不填表示从头到尾,左开右闭 print(test.endswith('x')) # 判断是否以x结尾 print(test.endswith('E', 2, 4)) print(test.startswith('a')) # 以a开始 test = 'alExalEx' print(test.find('E')) # 从前往后找寻找E出现在哪个位置(首次),可以加位置,返回-1代表没找到 print(test.index('E')) # 未找到就报错 test = 'I am {name}' print(test.format(name='alex')) # format格式化 print(test.format_map({'name': 'alex'})) test = 'afds324353dcz3fads5sd中' print('*******') print(test.isalnum()) # 只有数字和字母的时候返回True,汉字也可以 print(test.isalpha()) # 判断是否只是字母 s = 'fasd\t324\twklds' print(s.expandtabs(3)) # 三个一组寻找\t 找到之后剩余的用空格填充 test = '123' print(test.isdigit()) # 判断是否只是数字 print(test.isdecimal()) # 是否是数字 有局限 print(test.isnumeric()) test = '_qw12' # 字母 数字 下划线 print(test.isidentifier()) # 判断是否符合标识符 test = 'asdfh\tjfas\n' # 是否存在不可显示的字符 print(test.isprintable()) test = ' ' print(test.isspace()) # 判断是否是空格 test = 'hello world' print(test.title()) # 转换成标题 print(test.istitle()) # 判断是否是标题 test = '你是风儿我是沙' print(' '.join(test)) # 插入空格 print(test.center(20, '*')) # 设置宽度,一共20个位置,将test放中间,其他用*拼接 test = 'alexA' print(test.ljust(20, '*')) print(test.rjust(20, "*")) print(test.zfill(20)) # 前边用0填充 print(test.lower()) # 转化成小写 print(test.islower()) # 判断是否全部是小写 print(test.upper()) # 转化成大写 print(test.isupper()) # 判断是否是大写 test = ' ale x ' print(test.lstrip()) # 去除左边空格,换行 print(test.rstrip()) # 去除右边空格 print(test.strip()) # 去除两边空格 test = 'alex' print(test.lstrip('a')) # 去除a(以a开头) v = 'aeiuo' m = str.maketrans('aeiou', '12345') print(v.translate(m)) # 替换 test = 'alexafdsfffsiensfls' print(test.partition('s')) # 以s做分割 print(test.rpartition('s')) print(test.split('s')) # 分割 print(test.rsplit('s')) test = 'afsd\nfda' print(test.splitlines()) # 根据换行分割
python字符串魔法
猜你喜欢
转载自www.cnblogs.com/majianyu/p/10129616.html
今日推荐
周排行