码迷,mamicode.com
首页 > 编程语言 > 详细

python 字符串的魔法

时间:2018-02-02 18:35:48      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:com   star   line   区分大小写   dbf   code   pre   exp   use   

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 alex

test = "\talex "
print(v1)
alex

test = "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)
True

test = "backend"
v = test.endswith(‘D‘)
print(v)
False

swapcase 大小写任意切换

test = "alex"
v = test.swapcase()
print(v)
ALEX

test = "ALEX"
v = test.swapcase()
print(v)
alex

test = "alEX"
v = test.swapcase()
print(v)
ALex

python 字符串的魔法

标签:com   star   line   区分大小写   dbf   code   pre   exp   use   

原文地址:http://blog.51cto.com/innocence/2068249

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!