python字符串
python字符串(一)
字符串内置方法
- capitalize() 首字母大写
test = 'django'
print(test.capitalize()) #Django
- lower( ) 全小写 islower()判断全小写
- upper( ) 全大写 isupper()判断全大写
- casefold() 转换大小写(不仅限于英语)
test = 'dJango'
print(test.lower()) #django
print(test.casefold()) #django
- centrer 设置宽度,并将内容居中 ,两边填充
- ljust 设置宽度,内容居左,右边填充
- rjust 设置宽度,内容居右,左边填充
- zfill 设置宽度,内容居右,只能填充0
text = "Django"
print(test.center(20,"*"))
print(text.ljust(20,"*"))
print(text.rjust(20,"*"))
print(text.zfill(20))
*******DJango*******
Django**************
**************Django
00000000000000Django
20总长度,*空白内容填充
- swapcase 大写换小写,小写换大写
test = "dsaAFDdf"
print(test.swapcase())
DSAafdDF
- lstrip rstrip strip 去除空格,标识符(\t \n),和参数字符与该字符的最大公共子序列
- count 计算字符在字符串中出现的个数
test = 'dJango'
print(test.count("an")) #1
print(test.count("an",4)) #0
print(test.count("an",4,5)) #0
an需要查找的字符 4开始寻找的位置 5结束位置
- endswith 判断是否以该内容结尾
print(test.endswith("go",4)) #true
print(test.endswith("go",4,5)) #false
- startswith 判断是否以该内容开头
print(test.startswith("d",0)) #true
print(test.startswith("d",4,5)) #false
- find 寻找子序列在字符中的位置,若无或者下标越界,则返回-1
test = 'django'
print(test.find('a')) # 2
print(test.find('f')) # -1
print(test.find('j',0,1)) # -1
[0,1) 区间左闭又开
- index 寻找子序列在字符中的位置,若无或者下标越界,则抛出异常
test = 'django'
print(test.index('d')) # 0
print(test.index('d',1,2)) # ValueError: substring not found
- format 字符串格式化
sentence = "Who is {name}?"
print(sentence.format(name="Django")) #Who is Django?
print(sentence.format_map({'name' : 'Django'})) #Who is Django?
sentence = "Who is {0}?"
print(sentence.format("Django")) #Who is Django?
sentence.format_map({‘name’ : ‘Django’}) 只能根据名称不能通过下标
- isalnum 判断字符串中是否只包含字母和数字
print("jj998".isalnum()) # True
print("jj998+=".isalnum()) # False
- isalpha 判断是否只包含字母
text = 'abc'
print(text.isalpha()) #True
- isdecimal isdigit isnumeric 判断是否只为数字
- isalnum 判断是否含有数字
text = '二'
print(text.isalnum()) #True
print(text.isdecimal()) #False
print(text.isdigit()) #False
print(text.isnumeric()) #True
isdigit可以判断的范围不止是阿拉伯数字
- isprintable 是否存在不可显示的字符(\t \n)
print("123das".isprintable()) #True
print("123das\t".isprintable()) #False
- isspace 判断是否只有空格
print(" ".isspace()) #True
print("12 3455".isspace()) #False
- isidentifier 如果字符串是有效的 Python 标识符(字母,下划线,标识符class def.,.)返回 True,否则返回 False。
print( "if".isidentifier() ) #True
print( "def".isidentifier() ) #True
print( "class".isidentifier() ) #True
print( "_a".isidentifier() ) #True
print( "中国123a".isidentifier() ) #True
print( "123".isidentifier() ) # False
print( "3a".isidentifier() ) # False
print( "".isidentifier() ) # False
- istitle 判断是否满足标题书写规范,每个单词首字母大写
- title 将字符串转换为标题格式
text = "we havc reach mars In September"
print(text.istitle())
print(text.title())
False
We Havc Reach Mars In September
- expandtabs(n) 从字符串左边开始每n个为一组(若无\t),若有\t,则到/t为止,使那一组占n个位置(通常用于打印表格)
s = "12345678\t9".expandtabs(6)
print(s)
print(len(s))
text = "username\tpassword\taddress\nDjango\t123\txian\nSherlock\t123\tLondon\nWaston\t123\tAfgan"
print(text.expandtabs(20))
# 打印输出
username password address
Django 123 xian
Sherlock 123 London
Waston 123 Afgan
- join 根据间隔使字符串拼接
text="夜空中最亮的星"
print(" ".join(text))
# 间隔在前进行调用
- str.maketrans 按照映射关系创建替换词典
- tranlate 按照映射关系进行替换
m = str.maketrans("aoueo","12345")
text = "afsffageofd"
print(text.translate(m))
1fsff1g45fd
- partition 分割字符串,包含被分割的内容,总共分成3份
- rpartition 从右边开始分割字符串,包含被分割的内容,总共分成3份
- split 分割字符串,可设置分割次数
- rsplit 从右边开始分割字符串
test = "testoneandtwo"
print(test.partition('n'))
print(test.rpartition('n'))
print(test.split('n'))
print(test.split('n',1))
print(test.rsplit('n'))
('testo', 'n', 'eandtwo')
('testonea', 'n', 'dtwo')
['testo', 'ea', 'dtwo']
['testo', 'eandtwo']
['testo', 'ea', 'dtwo']
- splitlines 只按照换行符(\n)进行分隔,可设置布尔类型参数,True表示保留显示换行符,False不保留
test = "test\none\nand\ntwo"
print(test.splitlines(True))
['test\n', 'one\n', 'and\n', 'two']
- replace(‘a’,‘b’,2) 将a替换为b,一共替换2次
test = "opportunity"
print(test.replace("p","-"))
print(test.replace('p','-',1))
print(test.replace('p','-',2))
o--ortunity
o-portunity
o--ortunity