字符串的魔法

# int 将字符串转换成数字 type显示类型

num = '123213'
print(type(num))
print(type(int(num)))
num = 'a'
print(type(num))
——————————执行结果————————
<class 'str'>
<class 'int'>
<class 'str'>


#进制转换
num = '11'
print(int(num, base=8))
————————执行结果——————————
9

#最小二进制位长度

age = 10
print(age.bit_length())
——————————执行结果——————————
4


#首字母大写
capitalize
test = 'lin'print(test.capitalize())
——————————执行结果——————————
Lin


#小写转换

test = 'LIN'
print(test.lower())
print(test.casefold()) #高级转换,支持其他语言队友关系
——————————执行结果——————————
lin
lin

#居中显示,可自定义两边填充字符

test = 'lemon'
print(test.center(20))
print(test.center(20,'#'))
——————————执行结果——————————
lemon
#######lemon########

#计算字符串出现次数,可指定搜寻范围
test = 'lemon&luoluo'
print(test.count('l'))
print(test.count('l',0,7)) #从0开始到7结束
——————————执行结果——————————
3
2

#判断结尾字符
test = 'lemon'
print(test.endswith('n'))
——————————执行结果——————————
True

#寻找字符串位置,可自定义范围
test = 'leomn'
print(test.find('o'))
print(test.find('o',3,4))
——————————执行结果——————————
2
-1


#格式化占位符,为占位符传参
test = 'lemon{}{}'
print(test.format('&','luoluo'))
print('hello {name}'.format(name='lemon'))
print('{name} {age}'.format_map({'name':'lemon','age':18})) #传入字典
——————————执行结果——————————
lemon&luoluo
hello lemon
lemon 18

#寻找字符串,找不到会报错
test = 'lemon'
print(test.index('l'))
——————————执行结果——————————
0

#判断字符串是否只是包含数字和字母
test = 'lemon&luoluo'
print(test.isalnum())
print('lemon'.isalnum())
——————————执行结果——————————
False
True

#
格式化输出,通过\t制表符,自定义分割段长度
test = 'leomn\tluoluo'
print(test.expandtabs(10))
——————————执行结果————————
leomn     luoluo


#判断是否是字母,文字
test = 'lemon柠檬'
print(test.isalpha())
——————————执行结果——————————
True


#判断是否是数字
test = '123②二'
print(test.isdecimal()) #普通数字
print(test.isdigit()) #支持②
print(test.isnumeric())#支持中文
——————————执行结果——————————
False
False
True


#判断是否是【字母,数字,下划线】开头
test = '_lemon'
print(test.isidentifier())
——————————执行结果——————————
True


#判断是否存在输出不可见字符
test = 'lemon\tluoluo'
print(test)
print(test.isprintable())
——————————执行结果——————————
lemon luoluo
False


#判断是否是空格
test = ' '
print(test.isspace())
——————————执行结果——————————
True



#判断是否是标题(单词首字母大写)
test = 'Lemon And Luoluo'
print(test.istitle())
print('lemon and luoluo'.title().istitle())
——————————执行结果——————————
True
True



#字符串拼接,可自定义拼接符
test = '你是风儿我是沙'
print(test)
print(' '.join(test))
print('_'.join(test))
——————————执行结果——————————
你是风儿我是沙
你 是 风 儿 我 是 沙
你_是_风_儿_我_是_沙



#填充
test = 'lemon'
print(test.ljust(20,'*')) #左填充
print(test.rjust(20,'*')) #右填充
print(test.zfill(20)) #0填充
——————————执行结果——————————
lemon***************
***************lemon
000000000000000lemon



#大小写判断
test = 'Lemon'
print(test.islower())
print(test.isupper())
print(test.lower().islower())
print(test.upper().isupper())
——————————执行结果——————————
False
False
True
True



#去除可定义I的字符,默认是空白
test = ' lemon '
print(test.lstrip()) #处理左边
print(test.rstrip()) #处理右边
print(test.strip()) #两边都处理
#自定义去除,不能隔元素删除
print('aaalemonaa'.strip('a'))
print('aaabbbcccaaddbbdecceeaabbcc'.strip('cba'))
——————————执行结果——————————
lemon
lemon
lemon
lemon
ddbbdeccee
 
#根据自定义映射来替换
test = 'abcderqwwqredfs'
test2 = str.maketrans('aeiou','11111')
print(test.translate(test2))
——————————执行结果——————————
1bcd1rqwwqr1dfs



#将指定元素转换为分割
test = '1a2a3a4a5a6a7a8a'
print(test.partition('a'))
print(test.rpartition('a'))
print(test.split('a'))
print(test.rsplit('a'))
——————————执行结果——————————
('1', 'a', '2a3a4a5a6a7a8a')
('1a2a3a4a5a6a7a8', 'a', '')
['1', '2', '3', '4', '5', '6', '7', '8', '']
['1', '2', '3', '4', '5', '6', '7', '8', '']

--------------------------------------------
test2 ='123\n321\n1234567'
print(test2.splitlines())
print(test2.splitlines(True))
——————————执行结果——————————
['123', '321', '1234567']
['123\n', '321\n', '1234567']



#判断元素开头结尾
test = 'lemon'
print(test.endswith('n'))
print(test.startswith('l'))
——————————执行结果——————————
True
True


#大小写互换
test = 'LeMoN'
print(test.swapcase())
——————————执行结果——————————
lEmOn



#替换,需指定替换字符,可指定替换次数
test = 'lemon'
print(test.replace('l','A'))
TEST = 'LLLLL'
print(TEST.replace('L','0',3))
——————————执行结果——————————
Aemon
000LL


 
 
 
 
 
 



 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/lemonbk/p/10612742.html
今日推荐