python(字符串魔法)【三】

num='00011'
v=int(num,base=16)
print(v)

age=5
v1=age.bit_length()
print(v1)

以0填充【右边】
test='alex'
v2=test.zfill(20)
print(v2)


test='alexalex'
v3=test.count('ae')
print(v3)


断句(20字位符)
test = "username\temail\tpassword\nlaiying\[email protected]\t123\nlaiying\[email protected]\t123\nlaiying\[email protected]\t123"
v = test.expandtabs(20)
print(v)

从开始往后找,找到第一个之后,获取其位置

test='alexalex'
v4=test.find('al')
print(v4)

判断字符串中是否只含有数字和字母,单纯数字或者字母也可
test='123'
v5=test.isalnum()
print(v5)

test='asd火狐'
v5=test.isalpha()
print(v5)

test ='二' # 2, ②
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3)

是否存在不可显示的字符
\t 制表符
\n 换行

test = "oiu2sd\tfkj"
v = test.isprintable()
print(v)

输出false

判断是否全部是空格
test=' e'
v4=test.isspace()
print(v4)

输出False,全为空格时输出true

判断是否是标题(根据首字母大写判断)
test= 'Some of the magic of strings is troublesome'
v5=test.istitle()
print(v5)
v6=test.title()
print(v6)
v7=v6.istitle()
print(v7)

test = "Return True if all cased characters in S are uppercase and there is"
v1 = test.istitle()
print(v1)
v2 = test.title()
print(v2)
v3 = v2.istitle()
print(v3)
输出:False True


将字符串中每个字符用指定的分隔符拼接起来
test='如果S中的所有大小写字符都是大写字母,则返回True'
t='-'
v=t.join(test)
print(v)
输出:如-果-S-中-的-所-有-大-小-写-字-符-都-是-大-写-字-母-,-则-返-回-T-r-u-e


判断是否全部为小(大)写,转化为小(大)写
test = "Alex"
v1 = test.islower()
v2 = test.lower()
print(v1, v2)

v1 = test.isupper()
v2 = test.upper()
print(v1,v2)

移除指定字符串
有限最多匹配(先匹配多的字符块体)
test = "xa"
v = test.lstrip('xa')
v = test.rstrip('9lexxexa')
v = test.strip('xa')
print(v)

test.lstrip()
test.rstrip()
test.strip()

去除左右空白
v = test.lstrip()
v = test.rstrip()
v = test.strip()
print(v)
print(test)

去除\t \n
v = test.lstrip()
v = test.rstrip()
v = test.strip()
print(v)

对应关系替换
test1='12345'
test2='上山打老虎'
v='1456389741136363587412596'
m=str.maketrans('12345','上山打老虎')
new_a=v.translate(m)
print(new_a)


以XX元素将字符串分割为三部分含s分界点
test = "testasdsddfg"
v = test.partition('s')
print(v)
v = test.rpartition('s') #从右边开始以s为节点分割字符串
print(v)


test="testasdsddfg"
v=test.split('s') #以s为节点全部分开不含s分界点
print(v)
输出:['te', 'ta', 'd', 'ddfg']

大小写转换
test = "aLex"
v = test.swapcase()
print(v)
输出:AlEX

猜你喜欢

转载自www.cnblogs.com/huohu66888/p/9570616.html