python-字符串
- 转义字符
| \(在行尾时) | 续行符 |
| \\ | 反斜杠符号 |
| \' | 单引号 |
| \" | 双引号 |
| \a | 响铃 |
| \b | 退格(Backspace) |
| \000 | 空 |
| \n | 换行 |
| \v | 纵向制表符 |
| \t | 横向制表符 |
| \r | 回车 |
| \f | 换页 |
| \oyy | 八进制数,yy 代表的字符 |
| \xyy | 十六进制数,yy代表的字符 |
- 字符串格式化
print ("我叫 %s 今年 %d 岁!" % ('小明', 10))
# 我叫 小明 今年 10 岁!
字符串格式化符号(使用%作为格式化标志字符)
| %c | 格式化字符及其ASCII码 |
| %s | 格式化字符串 |
| %d | 格式化整数 |
| %u | 格式化无符号整型 |
| %o | 格式化无符号八进制数 |
| %x | 格式化无符号十六进制数 |
| %X | 格式化无符号十六进制数(大写) |
| %f | 格式化浮点数字,可指定小数点后的精度 |
| %e | 用科学计数法格式化浮点数 |
格式化操作符辅助指令(用在百分号和格式化符号之间)
| * | 定义宽度或者小数点精度 |
| - | 用做左对齐 |
| + | 在正数前面显示加号( + ) |
| <sp> | 在正数前面显示空格 |
| # | 在八进制数前面显示零('0'),在十六进制前面显示'0x'或者'0X' |
| 0 | 显示的数字前面填充'0'而不是默认的空 |
| % | '%%'输出一个单一的'%' |
| (var) | 映射变量(字典参数),直接使用变量名 |
| m.n. | m 是显示的最小总宽度,n 是小数点后的位数(如果可用的话) |
f-string格式化字符串以 f 开头,后面跟着字符串,字符串中的表达式用大括号 {} 包起来,它会将变量或表达式计算后的值替换进去
name = 'Runoob'
f'Hello {name}' # 使用变量
f'{1+2}' # 使用表达式
# '3'
w = {'name': 'Runoob', 'url': 'www.runoob.com'}
f'{w["name"]}: {w["url"]}' #使用键值
# 'Runoob: www.runoob.com'
如果需要对数据进行格式化,可以使用str.format()函数,通过 {} 和 : 来代替以前的 %
首先要保证后面的格式与前面的一致,比如字符串不能在格式化的时候使用f,需要提前使用float()转换
其格式为[[填充]对齐方式][正负号][#][0][宽度][分组选项][.精度][类型码]
所有的格式选项都放在:的后面,比如:18.2f表示宽度为18小数保留2位
填充只能是一个字符,不指定默认用空格填充,如果指定填充字符,必须同时指定对齐方式
<:左对齐,>:右对齐,^:居中,=:在正负号(如果有的话)和数字之间填充,该对齐选项仅对数字类型有效
#用于给各种进制的数字添加前缀
分组选项包括逗号,使用逗号对数字以千为单位进行分隔
类型码即为不同的进制对应的数值,其会进行转换
print('year={:0>4}'.format(year)) # 指定宽度为4,使用0填充右对齐
# 可以不添加s,也就是不进行类型的转换
"{} {}".format("hello", "world") # 不设置指定位置,按默认顺序
# 'hello world'
"{0} {1}".format("hello", "world") # 设置指定位置
# 'hello world'
"{1} {0} {1}".format("hello", "world") # 设置指定位置
# 'world hello world'
print("{:.2f}".format(3.1415926)); #如果需要格式化,则不能在括号内输入索引值,而是输入指定的格式,然后再按照位置进行匹配
# 3.14
- 内建函数
str.capitalize()
返回一个首字母大写的字符串,其他字符同时会变成小写
str = "this is string example from runoob....wow!!!"
print ("str.capitalize() : ", str.capitalize())
# str.capitalize() : This is string example from runoob....wow!!!
str.center(width[, fillchar]),width为字符串的总宽度,fillchar为填充字符
返回一个指定的宽度 width 居中的字符串,如果 width 小于字符串宽度直接返回字符串,否则使用 fillchar 去填充
str = "[runoob]"
print ("str.center(40, '*') : ", str.center(40, '*'))
# str.center(40, '*') : ****************[runoob]****************
str.count(sub, start= 0,end=len(string)),sub为搜索的字符串,start为字符串开始搜索的位置,end为字符串结束搜索的位置
该方法返回子字符串在字符串中出现的次数
str="www.runoob.com"
sub='o'
print ("str.count('o') : ", str.count(sub))
# str.count('o') : 3
sub='run'
print ("str.count('run', 0, 10) : ", str.count(sub,0,10))
# str.count('run', 0, 10) : 1
bytes.decode(encoding="utf-8", errors="strict"),encoding为要使用的编码,如"UTF-8",errors为设置不同错误的处理方案。默认为 'strict',意为编码错误引起一个UnicodeError。 其他可能得值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 值、
该方法返回解码后的字符串
同样的也有encode方法,其会返回编码后的字符串,是一个bytes对象(b'str')
str = "菜鸟教程";
str_utf8 = str.encode("UTF-8")
str_gbk = str.encode("GBK")
print(str)
# 菜鸟教程
print("UTF-8 编码:", str_utf8)
# UTF-8 编码: b'\xe8\x8f\x9c\xe9\xb8\x9f\xe6\x95\x99\xe7\xa8\x8b' #此处为bytes对象
print("GBK 编码:", str_gbk)
# GBK 编码: b'\xb2\xcb\xc4\xf1\xbd\xcc\xb3\xcc'
print("UTF-8 解码:", str_utf8.decode('UTF-8','strict'))
# UTF-8 解码: 菜鸟教程
print("GBK 解码:", str_gbk.decode('GBK','strict'))
# GBK 解码: 菜鸟教程
str.endswith(suffix[, start[, end]]),suffix可以是一个字符串或者是一个元素,start为字符串中的开始位置,end为字符中结束位置
如果字符串含有指定的后缀返回 True,否则返回 False
start 参数以 0 为第一个字符索引值
end 参数以 1 为第一个字符索引值
Str='Runoob example....wow!!!'
suffix='!!'
print (Str.endswith(suffix))
# True
print (Str.endswith(suffix,20))
# True
suffix='run'
print (Str.endswith(suffix))
# False
print (Str.endswith(suffix, 0, 19))
# False
str.expandtabs(tabsize=8),tabsize将指定转换字符串中的 tab 符号 \t 转为空格的字符数
该方法返回字符串中的 tab 符号 \t 转为空格后生成的新字符串
\t 是补全当前字符串长度到8的整数倍,最少 1 个最多 8 个空格,补多少要看你 \t 前字符串长度
比如当前字符串长度 10,那么 \t 后长度是 16,也就是补 6 个空格
如果当前字符串长度 12,此时 \t 后长度是 16,补 4 个空格
str = "runoob\t12345\tabc"
print('原始字符串:', str)
# 原始字符串: runoob 12345 abc
# 默认 8 个空格
# runnob 有 6 个字符,后面的 \t 填充 2 个空格
# 12345 有 5 个字符,后面的 \t 填充 3 个空格
print('替换 \\t 符号:', str.expandtabs())
# 替换 \t 符号: runoob 12345 abc
# 2 个空格
# runnob 有 6 个字符,刚好是 2 的 3 倍,后面的 \t 填充 2 个空格
# 12345 有 5 个字符,不是 2 的倍数,后面的 \t 填充 1 个空格
print('使用 2 个空格替换 \\t 符号:', str.expandtabs(2))
# 使用 2 个空格替换 \t 符号: runoob 12345 abc
# 3 个空格
print('使用 3 个空格:', str.expandtabs(3))
# 使用 3 个空格: runoob 12345 abc
# 4 个空格
print('使用 4 个空格:', str.expandtabs(4))
# 使用 4 个空格: runoob 12345 abc
# 5 个空格
print('使用 5 个空格:', str.expandtabs(5))
# 使用 5 个空格: runoob 12345 abc
# 6 个空格
print('使用 6 个空格:', str.expandtabs(6))
# 使用 6 个空格: runoob 12345 abc
str.find(str, beg=0, end=len(string)),str为指定检索的字符串,beg为开始的索引,默认为0,end为结束时的索引,默认为字符串的长度
如果包含子字符串返回开始的索引值,否则返回-1(此处只查找第一次出现的字符串)
另外也有一个index方法和此处类似,只不过不存在会抛出异常
str1 = "Runoob example....wow!!!"
str2 = "exam";
print (str1.find(str2))
# 7
print (str1.find(str2, 5))
# 7
print (str1.find(str2, 10))
# -1
str.isalnum()
如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False
str = "runoob2016" # 字符串没有空格
print (str.isalnum())
# True
str = "www.runoob.com" #含有标点符号
print (str.isalnum())
# False
str.isalpha()
如果字符串至少有一个字符并且所有字符都是字母或文字则返回 True,否则返回 False
str = "runoob"
print (str.isalpha())
# True
# 字母和中文文字
str = "runoob菜鸟教程"
print (str.isalpha())
# True
str = "Runoob example....wow!!!"
print (str.isalpha())
# False
str.isdigit()
如果字符串只包含数字则返回 True 否则返回 False
即不接受其他一切非 [0-9] 元素
str = "123456";
print (str.isdigit())
# True
str = "Runoob example....wow!!!"
print (str.isdigit())
# False
str.islower()
如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False
也即检测字符串是否不包含大写字母
str = "RUNOOB example....wow!!!"
print (str.islower())
# False
str = "runoob example....wow!!!"
print (str.islower())
# True
str.isnumeric()
如果字符串中只包含数字字符,则返回 True,否则返回 False
str = "runoob2016"
print (str.isnumeric())
# False
str = "23443434"
print (str.isnumeric())
# True
str.isspace()
如果字符串中只包含空白符,则返回 True,否则返回 False
空白符包括空格,制表符(\t),换行(\n),回车(\r)
空串不算空白符
str = " "
print (str.isspace())
# True
str = "Runoob example....wow!!!"
print (str.isspace())
# False
str.istitle()
如果字符串中所有的单词拼写首字母是否为大写,且其他字母为小写则返回 True,否则返回 False
str = "This Is String Example...Wow!!!"
print (str.istitle())
# True
str = "This is string example....wow!!!"
print (str.istitle())
# False
str.isupper()
如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False
str = "THIS IS STRING EXAMPLE....WOW!!!"
print (str.isupper())
# True
str = "THIS is string example....wow!!!"
print (str.isupper())
# False
str.join(sequence),sequence为要连接的元素序列,必须是字符串
返回通过指定字符连接序列中元素后生成的新字符串
使用这个字符来连接指定的序列
s1 = "-"
s2 = ""
seq = ("r", "u", "n", "o", "o", "b") # 字符串序列
print (s1.join( seq ))
# r-u-n-o-o-b
print (s2.join( seq ))
# runoob
len( s ),s为对象
返回对象长度
对象包括了字符,列表,元组等可以计数的对象
只要是对象,都可以计算长度。其不是字符串内的方法,有更广泛的应用
len(str) # 字符串长度
# 6
l = [1,2,3,4,5]
len(l) # 列表元素个数
# 5
str.ljust(width[, fillchar]),width指字符串的长度,fillchar为填充字符,默认为空格
返回一个原字符串左对齐(在右侧填充),并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串
str = "Runoob example....wow!!!"
print (str.ljust(50, '*'))
# Runoob example....wow!!!**************************
str.lower()
返回将字符串中所有大写字符转换为小写后生成的字符串
str = "Runoob EXAMPLE....WOW!!!"
print( str.lower() )
# runoob example....wow!!!
str.lstrip([chars]),chars为指定截取的字符
返回截掉字符串左边的空格或指定字符后生成的新字符串
str = " this is string example....wow!!! ";
print( str.lstrip() );
# this is string example....wow!!!
str = "88888888this is string example....wow!!!8888888";
print( str.lstrip('8') );
# this is string example....wow!!!8888888
str.maketrans(intab, outtab),intable为字符串中要替代的字符组成的字符串,outtable为相应的映射字符的字符串
返回字符串转换后生成的新字符串
这个转换表用于后面的translate进行转换
intab = "aeiou"
outtab = "12345"
trantab = str.maketrans(intab, outtab)
str = "this is string example....wow!!!"
print (str.translate(trantab))
# th3s 3s str3ng 2x1mpl2....w4w!!!
max(str),str为字符串
返回字符串中最大的字母(比较的是字母对应的unicode编码,其不但判断字母,而是判断所有字符对应的编码)
在有大小写的字符串中返回的是小写字母的最大值
同样的也有min(str),其在有大写字母的时候,返回的是小的大写字母
str = "runoob"
print ("最大字符: " + max(str))
# 最大字符: u
str.replace(old, new[, max]),old为将被替换的子字符串,new为新字符串,用于替换old子字符串,max表示替换不超过max次
返回字符串中的 old(旧字符串)替换成 new(新字符串)后生成的新字符串,如果指定第三个参数max,则替换不超过 max 次
先查找后替换
str = "www.w3cschool.cc"
print ("菜鸟教程旧地址:", str)
# 菜鸟教程旧地址: www.w3cschool.cc
print ("菜鸟教程新地址:", str.replace("w3cschool.cc", "runoob.com"))
# 菜鸟教程新地址: www.runoob.com
str = "this is string example....wow!!!"
print (str.replace("is", "was", 3))
# thwas was string example....wow!!!
str.rfind(str, beg=0, end=len(string)),str为查找的字符串,beg为开始查找的位置,end为结束的查找位置
返回字符串最后一次出现的位置,如果没有匹配项则返回-1
注意此为从右往左查找,这会影响到第一次查找到该子串的位置
str1 = "this is really a string example....wow!!!"
str2 = "is"
print (str1.rfind(str2))
# 5
print (str1.rfind(str2, 0, 10))
# 5
print (str1.rfind(str2, 10, 0))
# -1
print (str1.find(str2))
# 2
print (str1.find(str2, 0, 10))
# 2
print (str1.find(str2, 10, 0))
# -1
str.rindex(str, beg=0 end=len(string)),str为要查找的字符串,beg为开始查找的位置,end为结束查找的位置
返回子字符串 str 在字符串中最后出现的位置,如果没有匹配的字符串会报异常
此方法也是从右向左查找
str1 = "this is really a string example....wow!!!"
str2 = "is"
print (str1.rindex(str2))
# 5
print (str1.rindex(str2,10))
# Traceback (most recent call last):
File "test.py", line 6, in <module>
print (str1.rindex(str2,10))
ValueError: substring not found
str.rjust(width[, fillchar]),width为指定填充字符串的长度,fillchar为填充的字符,默认为空格
返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。如果指定的长度小于字符串的长度则返回原字符串
为了使其右对齐,则需要在左侧进行填充
str = "this is string example....wow!!!"
print (str.rjust(50, '*'))
# ******************this is string example....wow!!!
str.rstrip([chars]),chars为指定要删除的字符,默认为空格
返回删除 string 字符串末尾的指定字符后生成的新字符串
注意只能删除末尾的指定字符
str = " this is string example....wow!!! "
print (str.rstrip())
# this is string example....wow!!!
str = "*****this is string example....wow!!!*****"
print (str.rstrip('*'))
# *****this is string example....wow!!!
str.split(str="", num=string.count(str)),str为分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等,num为分割次数,默认为-1,即分割所有
返回分割后的字符串列表
str = "this is string example....wow!!!"
print (str.split( )) # 以空格为分隔符
# ['this', 'is', 'string', 'example....wow!!!']
print (str.split('i',1)) # 以 i 为分隔符
# ['th', 's is string example....wow!!!']
print (str.split('w')) # 以 w 为分隔符
# ['this is string example....', 'o', '!!!']
str.splitlines([keepends]),keepends为在输出结果里是否去掉换行符('\r', '\r\n', \n'),默认为 False,不包含换行符,如果为 True,则保留换行符
返回一个包含各行作为元素的列表
以换行符作为分割标记
'ab c\n\nde fg\rkl\r\n'.splitlines()
# ['ab c', '', 'de fg', 'kl']
'ab c\n\nde fg\rkl\r\n'.splitlines(True)
# ['ab c\n', '\n', 'de fg\r', 'kl\r\n']
str.startswith(substr, beg=0,end=len(string)),substr为指定的子字符串,beg设置字符串检测的起始位置,end用于设置字符串检测的结束位置
如果检测到字符串则返回True,否则返回False
str = "this is string example....wow!!!"
print (str.startswith( 'this' )) # 字符串是否以 this 开头
# True
print (str.startswith( 'string', 8 )) # 从第九个字符开始的字符串是否以 string 开头
# True
print (str.startswith( 'this', 2, 4 )) # 从第2个字符开始到第四个字符结束的字符串是否以 this 开头
# False
str.strip([chars]),chars为移除字符串头尾指定的字符序列,默认是删除空白符
返回移除字符串头尾指定的字符序列生成的新字符串
其不能删除中间部分的字符
str = "*****this is **string** example....wow!!!*****"
print (str.strip( '*' )) # 指定字符串 *
# this is **string** example....wow!!!
str.swapcase()
返回大小写字母转换后生成的新字符串
str = "this is string example....wow!!!"
print (str.swapcase())
# THIS IS STRING EXAMPLE....WOW!!!
str = "This Is String Example....WOW!!!"
print (str.swapcase())
# tHIS iS sTRING eXAMPLE....wow!!!
str.title()
返回"标题化"的字符串,就是说所有单词的首字母都转化为大写
str = "this is string example from runoob....wow!!!"
print (str.title())
# This Is String Example From Runoob....Wow!!!
txt = "hello b2b2b2 and 3g3g3g"
x = txt.title()
print(x)
# Hello B2B2B2 And 3G3G3G
str.translate(table)
bytes.translate(table[, delete])
bytearray.translate(table[, delete]),table为翻译表,delete为字符串中要过滤的字符列表
返回翻译后的字符串,若给出了 delete 参数,则将原来的bytes中的属于delete的字符删除,剩下的字符要按照table中给出的映射来进行映射
intab = "aeiou"
outtab = "12345"
trantab = str.maketrans(intab, outtab) # 制作翻译表
str = "this is string example....wow!!!"
print (str.translate(trantab))
# th3s 3s str3ng 2x1mpl2....w4w!!!
str.upper()
返回小写字母转为大写字母的字符串
str = "this is string example from runoob....wow!!!";
print ("str.upper() : ", str.upper())
# str.upper() : THIS IS STRING EXAMPLE FROM RUNOOB....WOW!!!
str.zfill(width),width为指定字符串的长度。原字符串右对齐,前面填充0,填充至指定长度
返回指定长度的字符串
str = "this is string example from runoob....wow!!!"
print ("str.zfill : ",str.zfill(40))
# str.zfill : this is string example from runoob....wow!!!
print ("str.zfill : ",str.zfill(50))
# str.zfill : 000000this is string example from runoob....wow!!!
str.isdecimal()
如果字符串是否只包含十进制字符返回True,否则返回False
str = "runoob2016"
print (str.isdecimal())
# False
str = "23443434"
print (str.isdecimal())
# True

浙公网安备 33010602011771号