最近在复习字符串,就把这些可能用到的分享一下,有的方法还挺有意思的,时常复习会发现自己的不足和遗忘,有时候一个知识点会牵扯出来一大堆自己不懂的知识点,这个时候就很烦,但还是得一个一个的慢慢理解,头发都快秃了塞=_=!
字符串进阶
字符串的操作方法:
isdecimal —> 判断一个字符串能否转换成10进制的整数
语法: str.isdecimal()
a = '4s'
print(a.isdecimal()) # ---> False
print('55'.isdecimal()) # ---> True
print('55.5'.isdecimal()) # ---> False
# 以下内容来自百度:
# 0x表示十六进制的int型变量
# \x表示十六进制的字符型变量
# 0x61 表示int型的97
# '\x61' 表示str型,ASCII码为十进制97的字符,即'a'
# b'\x61'表示bytes型的b'a'
print(0xA) # ---> 10
print('0xA'.isdecimal()) # ---> False,这里返回False的原因是0xa本意是10,但是变换成字符串就真的只是单纯的字符串了,也就不是10
isalnum —> 判断字符串当中,是否是 数字(0–9) 或者 英文字母(a–z A–Z) 的组合
语法: str.isalnum ()
print('aa'.isalnum()) # ---> True
print('55'.isalnum()) # ---> True
print('55.5'.isalnum()) # ---> False
print('0b11'.isalnum()) # ---> True
print('sss\nss'.isalnum()) # ---> Flase
# 没什么特殊的,有数字或者字母就Ture,没有就False
isalpha —> 判断字符串当中是否是纯英文字母,包括大小写
语法: str.isalpha()
print('abc'.isalpha()) # ---> True
print('ABC'.isalpha()) # ---> True,不分大小写
print('ABCabc'.isalpha()) # ---> True
print('abc123'.isalpha()) # --- False,不是纯纯的英文就FALSE
print('123'.isalpha()) # ---> False
isdigit —> 判断字符串当中是否只有数字
语法:str.isalpha()
print('sss'.isdigit()) # ---> False
print('1234'.isdight()) # ---> True
capitalize —> 将字符串的第一个字母变成大写,其他字母变小写。
(title —> 返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写)
语法:str.capitalize ()
print('python is best!'.capitalize()) # ---> Python is best!
print('python is best'.title) # ---> Python Is Best
upper —> 将字符串中的小写字母转为大写字母
语法:str.upper ()
print('python is best'.upper) # ---> PYTHON IS BEST
去空白strip —> 移除字符串头尾指定的字符(默认为空格)或字符序列
注意:
该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。
语法:str.strip()
print(' python is best! '.strip()) # ---> python is best!
print ('\t\npython is best!\t\n'.strip) # ---> python is best!
lstrip —> 用于截掉字符串左边的空格或指定字符.(左的英语:left)
语法:str.lstrip()
print(' python is best! '.lstrip()) ---> python is best!
print('\t\npython is best!\t\n'.lstrip) ---> python is best! (下面一行空白)只是去掉了左边的空白,右边的空白还是打印出来了
rstrip —> 用于删除字符串末尾的指定字符(默认为空格).(右的英语为:right)
语法:str.rstrip()
print(' python is best! '.rstrip()) ---> python is best!
print('\t\npython is best!\t\n'.rstrip) ---> python is best!(这行打印前面有一行的空白)
居中
center —> 填充至 width ,默认用空格填充,并且原字符居中
语法:str.center(总数,'用什么填充,default=空格')
print('python'.center(9))
# python
print('python'.center(40, '-'))
# --------------------python---------------------
print('center--居中'.center(40, '*'))
# ********************center--居中**********************
index —> 检测字符串中是否包含子字符串 str # str – 指定检索的字符串
找到的左起第一个位置索引
语法:str.index()
print('python'.index('y')) # ---> 1
print('python'.index('yth')) # ---> 1
print('python python'.index('y')) # ---> 1
print('python python'.index('yth')) # ---> 1
# 以下找不到就报错,报错:ValueError: substring not found(子字符串未找到)
print('python'.index('a'))
print('python'.index('b'))
print('python python'.index('abc'))
find —> 检测字符串中是否包含子字符串 str # – 指定检索的字符串,如果不包含索引值,返回 -1
找到的左起第一个位置索引
语法:str.find()
print('python'.index('y')) # ---> 1
print('python'.index('yth')) # ---> 1
print('python python'.index('yth')) # ---> 1,不管有几个,能找到就返回1
# 找不到就报错:ValueError: substring not found(子字符串未找到)
print('python'.index('a'))
print('python python'.index('abc'))
rfind —> "同上"
找到右起第一个位置索引′
print('python'.index('y')) # ---> 1
print('python'.index('yth')) # ---> 1
print('python p''y''thon'.index('y')) # ---> 1
print('python p''yth''on'.index('yth')) # ---> 1
# 以下找不到就报错:ValueError: substring not found(子字符串未找到)
print('python'.index('a'))
print('python'.index('b'))
print('python python'.index('abc'))
split —> 通过指定分隔符对字符串进行切片
切分语法:str.spilt()
参数:maxsplit=分隔几次(-1代表分隔最大次数),sep=用什么来切,切掉什么
str1 = 'python is best'
res = str1.split() # ---> 默认以空格来切
print(res)
# ['python', 'is', 'best']
# ----------------------------------------------------------
str2 = 'python is best! python is best'
res2 = str2.split('b') # ---> 以'b'来切,把'b'切掉
print(res2) #
# ['python is ', 'est! python is ', 'est']
# -------------------------------------------------------------
str3 = 'python is best! python is best'
res3 = str3.split(maxsplit=3) # ---> 分割3次,以空格来切
print(res3)
# ['python', 'is', 'best!', 'python is best']
# -------------------------------------------------------------
str4 = 'python is best! python is best'
res4 = str4.split(maxsplit=-1)
print(res4)
# ['python', 'is', 'best!', 'python', 'is', 'best']
# maxsplit: 最大分割次数 -1(默认值)表示没有限制
# ----------------------------------------------------------
str5 = 'python is best! python is best'
res5 = str5.split(maxsplit=1, sep='b') # ---> 分割1次 以'b'来切
print(res5)
# ['python is ', 'est! python is best']
join —> 用于将序列中的元素以指定的字符连接生成一个新的字符串。
语法:str.join(参数)
b = ' '.join('python') # 参数: 可迭代的 ==> 可遍历的 ==> 容器
print(b)
p y t h o n
-----------------------------------------------------
c = ' '.join['python', 'is', 'best!' ] # 容器中的每个元素用前边的字符串粘起来
print(c)
python is best!
print(type(c)) ---> <class 'str'>
谢谢参阅,如侵删