python 字符串的魔法
expandtabs()
test = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123" v = test.expandtabs(20) print(v) username email password laiying ying@q.com 123 laiying ying@q.com 123 laiying ying@q.com 123 \t 制表符 \n 换行 expandtabs(20)以20个字节为单位 遇到\t 自动补全,常用做制表
isalpha #判断是否为字母,汉字 有则为Ture
test = "asdf"v = test.isalpha()print(v)True test = "2asdf"v = test.isalpha()print(v)False
isdecimal isdigit 判断当前是否为数字isdigit()True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字False: 汉字数字Error: 无
isdecimal()True: Unicode数字,,全角数字(双字节)False: 罗马数字,汉字数字Error: byte数字(单字节)isnumeric()True: Unicode数字,全角数字(双字节),罗马数字,汉字数字False: 无Error: byte数字(单字节)
test = "②"v1 = test.isdecimal()v2 = test.isdigit()v2 = test.isnumeric()print(v1,v2,v3)False True True
isidentifier()变量名称标识符是否正确
a = "_123"v = a.isidentifier()print(v)True
isprintable 是否存在不可现实的字符
test = "adudbuwgdyu"v = test.isprintable()print(v)True
isspace 是否都是空格
test = " oo"v = test.isspace()print(v)False
istitle 是否为标题 标题的定义首字母为大写
test = "This Is All cased characters "v = test.istitle()print(v)False
title 把字符转换成标题首字母大写
test = "This Is All cased characters "v = test.title()print(v)This Is All Cased Characters
**join 将字符串中年的每一个元素按照指定分隔符进行拼接
test = "你是风儿我是沙"print(test)t = ' ' 间隔放在前面v = t.join(test)print(v)你是风儿我是沙你 是 风 儿 我 是 沙test = "你是风儿我是沙"print(test)v = "#".join(test)print(v)你是风儿我是沙你#是#风#儿#我#是#沙
ljust #内容放在了左边填充符放在了右边
test = "alex"v = test.ljust(20,"*")print(v)alex****************
rjust #内容放在了右边 填充字符放在了左边
test = "alex"v = test.rjust(20,"*")print(v)****************alex
zfill #默认以0为填充字符转 填充左边
test = "alex"v = test.zfill(20,)print(v)0000000000000000alex
islower,lower 判断是否全部为大小写和转化为大小写
test = "Alex"v1 = test.islower() 判断是否全部为小写v2 = test.lower() 全部转换为小写print(v1,v2)False alex
isupper upper
test = "Alex"v1 = test.isupper() 判断全部是否为大写v2 = test.upper() 全部转换位大写print(v1,v2)False ALEX
lstrip 处理左边的空格 rstrip 处理左边的空格 strip 取出空白 同时可以去除掉\t \n 的空白 还可以去除指定的字符(优先最多的匹配)
test = " alex "v1 = test.lstrip()v2 = test.rstrip()v3 = test.strip()print(v1,v2,v3)alex alex alextest = "\talex "print(v1)alextest = "xalex"v1 = test.lstrip('x')print(v1)alex
str.maketrans 根据对应关系进行替换
v = "asddedjihdhufhug;dihdubfyu;hdbfuuibifgi"m = str.maketrans("aeiou","12345")new_v = v.translate(m)print(new_v)1sdd2dj3hdh5fh5g;d3hd5bfy5;hdbf553b3fg3
partition 从左边第一个s进行分割字符串 rpartition 从右边开始分割 以s 且只能分割成三份;split 指定分隔符,且都能进行全部分割,但是分隔符号取不到;
test = "testadsdsdds"v = test.partition('s')print(v)('te', 's', 'tadsdsdds')test = "testadsdsdds"v = test.rpartition('s')print(v)('testadsdsdd', 's', '')test = "testadsdsdds"v = test.split('s')#test.rsplit()print(v)['te', 'tad', 'd', 'dd', '']test = "testadsdsdds"v = test.split('s',2) #指定到第几个分隔符print(v) ['te', 'tad', 'dsdds']
splitlines 根据换行分隔符,(true/false) 是否可以显示保留换行符号
test = "test\nadsdsnd\nisdhishd\nishdds"v = test.splitlines()print(v)['test', 'adsdsnd', 'isdhishd', 'ishdds']test = "test\nadsdsnd\nisdhishd\nishdds"v = test.splitlines(True)print(v)['test\n', 'adsdsnd\n', 'isdhishd\n', 'ishdds']
startswith 判断以什么字母开头区分大小写 为真则true 假则false;endswith判断以什么字符结尾
test = "backend 1.1.1.1"v = test.startswith('b')print(v)Truetest = "backend"v = test.endswith('D')print(v)False
swapcase 大小写任意切换
test = "alex"v = test.swapcase()print(v)ALEXtest = "ALEX"v = test.swapcase()print(v)alextest = "alEX"v = test.swapcase()print(v)ALex
共同学习,写下你的评论
评论加载中...
作者其他优质文章