前一章介绍了python中的集中基本数据类型,本章 着重 记录python中str字符串类型数据的应用。str字符串主要由两种方法,一种是方法,一种是魔术方法。由于内容实在过于多,本章只介绍其中的方法。我会按照pycharm给的内置方法顺序(即字母排列顺序) 全部 依次介绍各种方法的使用。
print(dir(str))
"""
'__add__', '__class__', '__contains__', '__delattr__',
'__dir__', '__doc__', '__eq__', '__format__', '__ge__',
'__getattribute__', '__getitem__', '__getnewargs__',
'__gt__', '__hash__', '__init__', '__init_subclass__',
'__iter__', '__le__', '__len__', '__lt__', '__mod__',
'__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', 'capitalize', 'casefold', 'center',
'count', 'encode', 'endswith', 'expandtabs', 'find',
'format', 'format_map', 'index', 'isalnum', 'isalpha',
'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric',
'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust',
'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind',
'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split',
'splitlines', 'startswith', 'strip', 'swapcase', 'title',
'translate', 'upper', 'zfill'
"""
# 前后有两个下划线的为魔术方法,剩下的则是我们这一节讨论的方法。
首先丢出这节的目录:
目录
首先丢出这节的目录:
1、capitalize()
2、casefold()
3、center()
4、count()
5、encode()
6、endswith()
7、expandtabs()
8、find()
9、format
10、format_map(maping)
11、index
12、isalnum
13、isalpha
14、isdecimal
15、isdigit
16、isidentifier
17、islower
18、isnumeric
19、isprintable
20、isspace
21、istitle
22、isupper
23、join
24、ljust
25、lower
26、lstrip
27、maketrans
28、partition
29、replace
30、rfind
31、rindex
32、rjust
33、rpartition
34、rsplit
35、rstrip
36、split
37、splitlines
38、startswith
39、strip
40、swapcase
41、title
42、translate
43、upper
44、zfill
1、capitalize()
还是老规矩,看看pycharm对于capitalize方法给出的解释。大致意思第一个字符就是返回一个大写的版本,其他的还是小写
def capitalize(self): # real signature unknown; restored from __doc__
"""
S.capitalize() -> str
Return a capitalized version of S, i.e. make the first character
have upper case and the rest lower case.
"""
return ""
capitalize()具体使用情况如下
a = "life is too short,I use Python"
print(a) # life is too short,I use Python
print(a.capitalize()) # Life is too short,i use python
s = "人生苦短,我用Python"
print(s) # 人生苦短,我用Python
print(s.capitalize()) # 人生苦短,我用python
s1 = "人生苦短,我用python"
print(s1) # 人生苦短,我用python
print(s1.capitalize()) # 人生苦短,我用python
由上面两个变量可以看出。capitalize()是返回一个字符串副本,并将第一个字符大写。假如第一个字符不是英文,会将后面第一个英文字符进行小写化,若是小写的存在则保持小写状态,不进行改变。
2、casefold()
对于casefold()方法,pycharm给出的解释是:
def casefold(self): # real signature unknown; restored from __doc__
"""
S.casefold() -> str
Return a version of S suitable for caseless comparisons.
"""
意思是返回经过标准化(normalize)后的字符串,标准化类似于转换为小写,但更适合用于对Unicode字符串进行不区分大小写的比较,和我们众所周知的lower()方法不同,casefold可以将对于其他语言(非汉语或英文)中把大写转换为小写。如下面的代码块中的 西塔 Theta 属于希腊字符就发生了改变。
a = "PYTHON"
print(a) # PYTHON
print(a.casefold()) # python
c = "Θ"
print(c) # Θ
print(c.casefold()) #θ
3、center()
顺序介绍第三个就是center,中心中央这个单词,那么来看看pycharm给我们的对于它的解释是什么呢?
def center(self, width, fillchar=None): # real signature unknown; restored from __doc__
"""
S.center(width[, fillchar]) -> str
Return S centered in a string of length width. Padding is
done using the specified fill character (default is a space)
"""
return ""
返回一个长度为(len(string),width)的字符串。这个字符串的中间包括当前字符串,但两端用fillchar指定的字符(默认是空格)填充。注意:“双引号”可以不存在,那么就是默认空字符串填写在原str前后两端,若写了“双引号”并没有添加空格,则会出现报错提醒,如代码块:
# 语法:str.center(width , "fillchar") -> str 返回字符串
a = "hello world"
print(a) # hello world
print(a.center(20,"*")) # ****hello world*****
b = "hello world"
print(b.center(20,"")) #TypeError: The fill character must be exactly one character long
4、count()
第四个是count,这个方法还是比较常见的一种方法在各个编程语言中,统计某个字符在这个字符串中出现的次数,可搜索范围限定为string[start :end] , 其中pycharm给的解释是:
def count(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.count(sub[, start[, end]]) -> int
Return the number of non-overlapping occurrences of substring sub in
string S[start:end]. Optional arguments start and end are
interpreted as in slice notation.
"""
return 0
那么在实际的环境中,应用的效果:
# S.count(sub[, start[, end]]) -> int
s = "color red yellow green blue black white purple"
print(s.count("o", 0, )) #3
print(s.count("o")) # 3
print(s.count("o", ,10)) # SyntaxError: invalid syntax
将指定字符串找到后,用上count() 方法,括号里面第一个字符用“双引号”包含在其中,中间的start为指定开始的位置,从你指定位置开始向后寻找,指定查找的字符,end为终止节点,此位置后的字符串将不考虑在查找范围内,当然我们可以填了start后空出end的位置,那么就自动匹配到字符串结束。假如start & end 都不存在,则是对整个字符串进行搜索。 假如start的位置空出,只填写sub & end 的话,会报错的。
5、encode()
对于这个encode() 方法,看单词就知道这个方法跟编码有关。pycharm给的解释:返回使用指定编码和errors指定的错误处理方式对字符串进行编码的结果。
def encode(self, encoding='utf-8', errors='strict'): # real signature unknown; restored from __doc__
"""
S.encode(encoding='utf-8', errors='strict') -> bytes
Encode S using the codec registered for encoding. Default encoding
is 'utf-8'. errors may be given to set a different error
handling scheme. Default is 'strict' meaning that encoding errors raise
a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
'xmlcharrefreplace' as well as any other name registered with
codecs.register_error that can handle UnicodeEncodeErrors.
"""
return b""
encoding参数中有utf-8、utf-16、utf-32、gbk、ASCII 码,参数errors的值可能取值包含 ‘strict’ 、‘ignore’ 、 ‘replace’等。
# encode()语法:str.encode(encoding='UTF-8',errors='strict')
str = "hello world!"
print(str.encode("gbk","strict")) # b'hello world!'
print(str.encode("utf-8", "strict")) # b'hello world!'
print(str.encode("utf-16","strict"))
# b'\xff\xfeh\x00e\x00l\x00l\x00o\x00 \x00w\x00o\x00r\x00l\x00d\x00!\x00'
print(str.encode("utf-32","strict"))
# b'\xff\xfe\x00\x00h\x00\x00\x00e\x00\x00\x00l\x00\x00\x00l\x00\x00\x00o\x00\x00\x00 \x00\x00\x00w\x00\x00\x00o\x00\x00\x00r\x00\x00\x00l\x00\x00\x00d\x00\x00\x00!\x00\x00\x00'
6、endswith()
endswitch()方法是用于检查字符串是否以suffix结尾,还可使用索引start和end来指定匹配范围,如果以指定后缀结尾返回True,否则返回False。pycharm的使用方法 & 解释为:
def endswith(self, suffix, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.endswith(suffix[, start[, end]]) -> bool
Return True if S ends with the specified suffix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
suffix can also be a tuple of strings to try.
"""
return False
endswith()方法语法:
S.endswith(suffix[, start[, end]]) -> bool
具体用法如下:
a = "hello world!"
print(a.endswith("d")) # False
print(a.endswith("!", 0)) # True
print(a.endswith("ll", 2, 4)) # True
print(a[2:4]) # ll
这个方法若用于检查最后字符串最后一位的话,无须填写start & end,默认就好了。若检验中间的指定字符串用法就类似于list中的切片,用start & end 截取出指定范围并进行校对。
7、expandtabs()
返回将字符串中的制表符展开为空格后的结果,可指定可选参数tabsize(默认为8),说通俗一点就是把字符串中的 tab 符号('\t')转为空格,tab 符号('\t')默认的空格数是 8。pycharm的使用方法 & 解释为:
def expandtabs(self, tabsize=8): # real signature unknown; restored from __doc__
"""
S.expandtabs(tabsize=8) -> str
Return a copy of S where all tab characters are expanded using spaces.
If tabsize is not given, a tab size of 8 characters is assumed.
"""
return ""
expandtabs()方法语法为:
S.expandtabs(tabsize=8) -> str
具体方法如下:
str = "this\tis python ! "
print(str.expandtabs(8)) # this is python !
print(str.expandtabs(12)) # this is python !
print(str.expandtabs(16)) # this is python !
该方法返回字符串中的 tab 符号('\t')转为空格后生成的新字符串。转换的长度随expandtabs中参数变化而变化。
8、find()
返回找到的第一个子串sub的索引,如果没有找到这样的子串,就返回-1;还可将搜索范围限制为string[start : end]
def find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.find(sub[, start[, end]]) -> int
Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.
"""
return 0
具体的格式: S.find(sub[, start[, end]]) -> int;str -- 是检索的字符串;start -- 开始索引,默认为0;end -- 结束索引,默认为字符串的长度 。 那么运行起来是一个这样的一个情况:
# S.find(sub[, start[, end]]) -> int
a = "life is short , i use python."
print("字母i在{}位置上".format(a.find("i"))) # 字母i在1位置上
print('use在{}位置上'.format(a.find('s',14,15))) # use在-1位置上
print('use在{}位置上'.format(a.find('s',14,20))) # use在19位置上
首先我们先找的字母i,find()函数自动会检查到第一个符合我们指定的字符并返回位置,第二个print输出为指定的字符串,并且指定了开始索引和结束索引,但是在这个范围内,并没有找到符合的字符串,所以返回值为-1,第三个print输出则返回的是一个匹配正确的字符串的首字母u的位置。即use中的u字母在19的位置上。
9、format
实现了标准的Python字符串格式设置。将字符串中用大括号分割的字段替换为相应的参数,再返回结果。
具体用法就像上面的使用的代码块。pycharm给的用法解释为:
def format(self, *args, **kwargs): # known special case of str.format
"""
S.format(*args, **kwargs) -> str
Return a formatted version of S, using substitutions from args and kwargs.
The substitutions are identified by braces ('{' and '}').
"""
pass
10、format_map(maping)
类似于使用关键字参数调用format,只是参数是映射的方式提供的。即mapping是一个字典对象。
def format_map(self, mapping): # real signature unknown; restored from __doc__
"""
S.format_map(mapping) -> str
Return a formatted version of S, using substitutions from mapping.
The substitutions are identified by braces ('{' and '}').
"""
return ""
# S.format_map(mapping) -> str
Student = {"id":"001","name":"Bob","age":"18"}
print("My name is {name},I'm {age} old ,my id is {id}".format_map(Student))
# My name is Bob,I'm 18 old ,my id is 001
print("My name is {name},I'm {age} old ".format_map(Student))
# My name is Bob,I'm 18 old
于format不同,format是将后面的参数插入前面输出的大括号中,参数类型可以是str、list、数字等。但是format_map()中的参数只能是键值对其中的value值,你可以设置了多个键值对,不必全部输出。选择想输出的value在大括号中填入其对应的key值即可。
11、index
index(sub[, start[, end]])返回找到的第一个子串sub的索引,如果没有找到这样的子串,将引发valueError异常;还可将搜索范围限制为string[start:end]。
def index(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.index(sub[, start[, end]]) -> int
Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation.
Raises ValueError when the substring is not found.
"""
return 0
s = "If we just open the door a crack,the light comes pouring in."
print("open中o字母在{}位置上".format(s.index("open")))
print(s.index("open", 0, 1)) # ValueError: substring not found
index()方法和第8个提到的find()方法很相似,都是在一定长度的字符串中查找规定子串的首次出现位置。不同点在于,index直接返回ValueError异常,而find返回值为-1。
12、isalnum
isalnum()检查字符串中的字符是否 都为字母或数 ,满足则返回Ture,不满足返回False。
def isalnum(self): # real signature unknown; restored from __doc__
"""
S.isalnum() -> bool
Return True if all characters in S are alphanumeric
and there is at least one character in S, False otherwise.
"""
return False
a = 'd1231dsa1'
print(a.isalnum()) # True
b = "$asd%gq12"
print(b.isalnum()) # False
很好理解。
13、isalpha
isalpha()检查字符串中的字符是否 都是字母 。用法如上12,isalnum方法。
# S.isalpha() -> bool
a = "hello"
print(a.isalpha()) # True
14、isdecimal
isdecimal()检查字符串中的字符是否都是十进制的数,用法如上12,isalnum方法。注:python中二进制为0o开头,八进制数位0x开头,16进制数位0b开头。
# S.isdecimal() -> bool
a = oct(64)
print(a, a.isdecimal()) # 0o100 False
b = hex(64)
print(b, b.isdecimal()) # 0x40 False
c = bin(64)
print(c, c.isdecimal()) # 0b1000000 False
d = '64'
print(d, d.isdecimal()) # 64 True
15、isdigit
isdigit()检查字符串中的字符是否都是数字。
# S.isdigit() -> bool
a = "\t2t123"
print(a.isdigit()) # False
a = "123"
print(a.isdigit()) # Ture
16、isidentifier
isidentifier()检查字符串是否可用作Python标识符。在python里,标识符有字母、数字、下划线组成。所有标识符可以包括英文、数字以及下划线(_),但不能以数字开头。标识符可以简单的理解成为一个文件命名的规范。
# S.isidentifier() -> bool
a = "\t"
print(a.isidentifier()) # False
a = "1"
print(a.isidentifier()) # False
17、islower
islower()检查字符串中 所有字母都是小写 的。不管字符串中有没有其他东西,比如数字,汉字,符号等等。
# S.islower() -> bool
a = 'abc'
print(a.islower()) # True
a = 'Abc'
print(a.islower()) # False
a = "123abc"
print(a.islower()) # True
18、isnumeric
isnumeric()检查字符串中的所有字符是否 都是数字字符 。
# S.isnumeric() -> bool
a = '\t\n'
print(a.isnumeric()) # False
a = '123'
print(a.isnumeric()) # Ture
a = '123\t'
print(a.isnumeric()) # False
a = "hello world"
print(a.isnumeric()) # False
19、isprintable
isprintable()检查字符串中的字符是否都是可打印的。如\t 制表符 , \n换行符是无法打印的。
#S.isprintable() -> bool
a = '\t\t\t'
print(a.isprintable()) # False
a = '\t\n'
print(a.isprintable()) # False
a = '123\t'
print(a.isprintable()) # False
20、isspace
isspace()检查字符串中的字符都是空白字符。
#S.isspace() -> bool
a = ' '
print(a.isspace()) # True
a = '00'
print(a.isspace()) # False
a = '\t\t\t'
print(a.isspace()) # True
a = '\t\n'
print(a.isspace()) # True
21、istitle
istitle()检查字符串中位于非字母后面的字母都是大写的,且其他所有字母都是小写的。我的理解就是检查开头第一个英文字符是不是大写。
# S.istitle() -> bool
a = "56赋值A%&*@13 23A"
print(a.istitle()) # True
a = "asad1AVB"
print(a.istitle()) # False
a = "hello world!"
print(a.istitle()) # False
a = "Hello World!"
print(a.istitle()) # True
22、isupper
isupper()检查字符串中的字母是否都是大写。
# S.isupper() -> bool
a = "hello world!"
print(a.isupper()) # False
a = "HELLO WORLD!"
print(a.isupper()) # True
"""
对于上面的一类以is开头的方法,就是检验是否满足
其后面剩余的字符的方法,如islower拆成is & lower
其中lower方法会在后面介绍,是一种将字符串中大写字符
转换成小写的一个方法。
"""
23、join
join()方法,将string与sequence中所有的字符串元素合并生成的新字符串,并返回结果。两字符串相连
def join(self, iterable): # real signature unknown; restored from __doc__
"""
S.join(iterable) -> str
Return a string which is the concatenation of the strings in the
iterable. The separator between elements is S.
"""
return ""
a = "——"
b = "abc"
print(a.join(b)) # a——b——c
24、ljust
ljust()返回一个长度为max(len(string),width)的字符串,其开头是当前字符串的副本,而末尾是使用fillchar指定的字符(默认为空格)填充的。
- width -- 指定字符串长度。
- fillchar -- 填充字符,默认为空格。
def ljust(self, width, fillchar=None): # real signature unknown; restored from __doc__
"""
S.ljust(width[, fillchar]) -> str
Return S left-justified in a Unicode string of length width. Padding is
done using the specified fill character (default is a space).
"""
return ""
a = "hello"
print(a.ljust(10,'c')) # helloccccc
显示结果为10个字符长度的字符串,在原字符串的基础上增加到了10(原本是5,增加了5个字符)。
25、lower
lower()将字符串中所有的字母都转换成小写,并返回结果。
def lower(self): # real signature unknown; restored from __doc__
"""
S.lower() -> str
Return a copy of the string S converted to lowercase.
"""
return ""
a = "HELLO WORLD!"
print(a.lower()) # hello world!
a = "hello world!"
print(a.lower()) # hello world!
26、lstrip
lstrip([chars])将字符串开头所有的chars(默认为所有的空白字符,如空格、制表符和换行符)都删除,并返回结果。删除完后自动缩进定格。
def lstrip(self, chars=None): # real signature unknown; restored from __doc__
"""
S.lstrip([chars]) -> str
Return a copy of the string S with leading whitespace removed.
If chars is given and not None, remove characters in chars instead.
"""
return ""
a = "aaaaaaaaaaaaahello world!"
print(a.lstrip("a")) # hello world!
a = " hello world!"
print(a.lstrip()) # hello world!
27、maketrans
一个静态方法,创建一个供translate使用的转换表。如果只指定了参数x,它必须是从字符或序数到Unicode序数或None(用于删除)的映射;也可使用两个表示源字符和目标字符串调用它;还可以提供第三个参数,它指定要删除的字符。
def maketrans(self, *args, **kwargs): # real signature unknown
"""
Return a translation table usable for str.translate().
If there is only one argument, it must be a dictionary mapping Unicode
ordinals (integers) or characters to Unicode ordinals, strings or None.
Character keys will be then converted to ordinals.
If there are two arguments, they must be strings of equal length, and
in the resulting dictionary, each character in x will be mapped to the
character at the same position in y. If there is a third argument, it
must be a string, whose characters will be mapped to None in the result.
"""
pass
str.maketrans(intab, outtab)
- intab -- 字符串中要替代的字符组成的字符串。
- outtab -- 相应的映射字符的字符串。
intab = "aei"
outtab = "123"
deltab = "to"
trantab1 = str.maketrans(intab , outtab) # 创建字符映射转换表
trantab2 = str.maketrans(intab,outtab,deltab) #创建字符映射转换表,并删除指定字符
test = "this is string example....wow!!!"
print(test.translate(trantab1)) # th3s 3s str3ng 2x1mpl2....wow!!!
print(test.translate(trantab2)) # h3s 3s sr3ng 2x1mpl2....ww!!!
28、partition
在字符串中搜索sep,并返回元祖(sep前面的部分,sep,sep后面的部分)。pycharm给的解释:
def partition(self, sep): # real signature unknown; restored from __doc__
"""
S.partition(sep) -> (head, sep, tail)
Search for the separator sep in S, and return the part before it,
the separator itself, and the part after it. If the separator is not
found, return S and two empty strings.
"""
pass
a = "good"
b = a.partition('o')
print(b, type(b)) # ('g', 'o', 'od')
# 记住tuple不能修改
29、replace
将字符串中的子串old替换成new,并返回结果;还可将最大替换次数限制为max。S.replace(old, new[, count]) -> str,old为原字符串的子串,new为即将替换的子串。replace在字符串的使用中频率是比较高的一类。
def replace(self, old, new, count=None): # real signature unknown; restored from __doc__
"""
S.replace(old, new[, count]) -> str
Return a copy of S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
"""
return ""
print("this is a test".replace('this', 'This')) # This is a test
30、rfind
返回找到的最后一个子串的索引,如果没有找到这样的子串,就返回-1;还可将搜索范围限定为string[start : end]。S.rfind(sub[, start[, end]]) -> int,sub为规定子串,start为开始索引,end为结束索引。
def rfind(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.rfind(sub[, start[, end]]) -> int
Return the highest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.
"""
return 0
print("This is a test".rfind('s', 0, 1)) # -1
print("This is a test".rfind('s')) # 12
rfind和find的返回相同,错误则返回-1,正确的找到则返回满足要求的索引值。
不同点:find 为第一个子串的索引,rfind为最后一个子串的索引,r即right(右,从后往前读)
31、rindex
rindex(sub[, start[, end]])返回找到的 最后 一个子串sub的索引,如果没有找到这样的子串,将引发valueError异常;还可将搜索范围限制为string[start:end]。
def rindex(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.rindex(sub[, start[, end]]) -> int
Return the highest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation.
Raises ValueError when the substring is not found.
"""
return 0
s = "If we just open the door a crack,the light comes pouring in."
print("t字母在{}位置上".format(s.rindex("t"))) # t字母在41位置上
print(s.index("t", 0, 1)) # ValueError: substring not found
rindex和index的返回相同,错误则返回ValueError异常,正确的找到则返回满足要求的索引值。
不同点:rindex 为第一个子串的索引,index 为最后一个子串的索引,r即right(右,从后往前读)
32、rjust
返回一个长度为max(len(String),width)的字符串。其末尾是当前字符串的拷贝,而开头是使用fillchar指定的字符(默认为空格)填充的。
- width -- 指定字符串长度。
- fillchar -- 填充字符,默认为空格。
def rjust(self, width, fillchar=None): # real signature unknown; restored from __doc__
"""
S.rjust(width[, fillchar]) -> str
Return S right-justified in a string of length width. Padding is
done using the specified fill character (default is a space).
"""
return ""
a = "aaaahello world!aaaa"
print(a.lstrip("a")) # hello world!aaaa
a = " hello world! "
print(a.lstrip()) # hello world!
注意:ljust ——L = left , rjust —— R = right
33、rpartition
与partition相同,但从右往左搜索。将字符串按照要求切成三段类似于切片。
def rpartition(self, sep): # real signature unknown; restored from __doc__
"""
S.rpartition(sep) -> (head, sep, tail)
Search for the separator sep in S, starting at the end of S, and return
the part before it, the separator itself, and the part after it. If the
separator is not found, return two empty strings and S.
"""
pass
a = "good"
b = a.rpartition('o')
print(b, type(b)) # ('go', 'o', 'd')
# 记住tuple不能修改
34、rsplit
与split相同,但指定了参数maxsplit,从右往左计算划分次数。
def rsplit(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__
"""
S.rsplit(sep=None, maxsplit=-1) -> list of strings
Return a list of the words in S, using sep as the
delimiter string, starting at the end of the string and
working to the front. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified, any whitespace string
is a separator.
"""
return []
a = 'good\tfood\tnood\taood'
print(a.rsplit()) # ['good', 'food', 'nood', 'aood']
print(a.rsplit("\t",2)) # ['good\tfood', 'nood', 'aood']
35、rstrip
将字符串末尾所有的chars字符(默认为所有的空白字符,如空格、制表符、换行符)都删除,并返回结果。
def rstrip(self, chars=None): # real signature unknown; restored from __doc__
"""
S.rstrip([chars]) -> str
Return a copy of the string S with trailing whitespace removed.
If chars is given and not None, remove characters in chars instead.
"""
return ""
a = "hello world!!!!"
print(a.rstrip("!")) # hello world
a = "hello world!\t\t\t\n"
print(a.rstrip()) # hello world!
注意:rstrip为从右(right)往左右删规定字符。strip为从左(默认)往右删除规定字符。
36、split
split()通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔 num+1 个子字符串。
def split(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__
"""
S.split(sep=None, maxsplit=-1) -> list of strings
Return a list of the words in S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are
removed from the result.
"""
return []
a = 'good\tfood\tnood\naood'
print(a.split()) # ['good', 'food', 'nood', 'aood']
print(a.rsplit()) # ['good', 'food', 'nood', 'aood']
print(a.split("\t",2)) # ['good', 'food', 'nood\naood']
split 和rsplit的用法差不多,唯一的区别就是它们读取字符串的顺序,split默认从左向右分开,rsplit则从右向左分开。和strip、rstrip的区分一样。
37、splitlines
splitlines按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。
其中参数keepends -- 在输出结果里是否保留换行符('\r', '\r\n', \n'),默认为 False,不包含换行符,如果为 True,则保留换行符。
def splitlines(self, keepends=None): # real signature unknown; restored from __doc__
"""
S.splitlines([keepends]) -> list of strings
Return a list of the lines in S, breaking at line boundaries.
Line breaks are not included in the resulting list unless keepends
is given and true.
"""
return []
a = "abc\n\n\td ef\nghi \tjk"
print(a)
# abc
#
# d ef
# ghi jk
print(a.splitlines()) # ['abc', '', '\td ef', 'ghi \tjk']
b = "abc\n\n\td ef\nghi \tjk"
print(a.splitlines(True)) # ['abc\n', '\n', '\td ef\n', 'ghi \tjk']
38、startswith
startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。
startswith 用法 : S.startswith(prefix[, start[, end]]) -> bool
- str -- 检测的字符串。
- strbeg -- 可选参数用于设置字符串检测的起始位置。
- strend -- 可选参数用于设置字符串检测的结束位置。
def startswith(self, prefix, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.startswith(prefix[, start[, end]]) -> bool
Return True if S starts with the specified prefix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
prefix can also be a tuple of strings to try.
"""
return False
a = "Other men live to eat, while I eat to live"
print(a.startswith("Other")) # True
print(a.startswith("O")) # True
print(a.find("men")) # 6
print(a.startswith("men", 6, 9)) # True
39、strip
strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。str.strip([chars])中chars -- 移除字符串头尾指定的字符序列。
def strip(self, chars=None): # real signature unknown; restored from __doc__
"""
S.strip([chars]) -> str
Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.
"""
return ""
a = "123apple123"
print(a.strip("123")) # apple
a = "123ap123ple123"
print(a.strip("123")) # ap123ple
结果显示:strip只能删除头尾指定的字符串,对字符串中的字符删除不了。
40、swapcase
sqapcase()用于对字符串的大小写字母进行转换。
def swapcase(self): # real signature unknown; restored from __doc__
"""
S.swapcase() -> str
Return a copy of S with uppercase characters converted to lowercase
and vice versa.
"""
return ""
a = "Life is short, just ues Python."
print(a.swapcase()) # lIFE IS SHORT, JUST UES pYTHON.
a = "hello WORLD!\n你好世界!"
print(a.swapcase())
# HELLO world!
# 你好世界!
41、title
title() 方法返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())。
def title(self): # real signature unknown; restored from __doc__
"""
S.title() -> str
Return a titlecased version of S, i.e. words start with title case
characters, all remaining cased characters have lower case.
"""
return ""
a = "life is short, just use Python"
print(a.title()) # Life Is Short, Just Use Python
print((a.title()).istitle()) # True
istitle是来判断每个单词是不是大写开头,那么我拿到后先用title转换成每个单词首字母大写,再用istitle自然满足istitle的判断要求并输出True。
42、translate
translate() 方法根据参数table给出的表(包含 256 个字符)转换字符串的字符, 要过滤掉的字符放到 del 参数中。
translate()方法语法:str.translate(table[, deletechars]);
- table -- 翻译表,翻译表是通过maketrans方法转换而来。
-
deletechars -- 字符串中要过滤的字符列表。
def translate(self, table): # real signature unknown; restored from __doc__ """ S.translate(table) -> str Return a copy of the string S in which each character has been mapped through the given translation table. The table must implement lookup/indexing via __getitem__, for instance a dictionary or list, mapping Unicode ordinals to Unicode ordinals, strings, or None. If this operation raises LookupError, the character is left untouched. Characters mapped to None are deleted. """ return ""
43、upper
upper()方法将字符串中的小写字母转为大写字母。
def upper(self): # real signature unknown; restored from __doc__
"""
S.upper() -> str
Return a copy of S converted to uppercase.
"""
return ""
a = "hello world!"
print(a.upper()) # HELLO WORLD!
44、zfill
zfill()方法是最后一个方法,陆陆续续的写终于写到了最后一个方法。 zfill() 方法返回指定长度的字符串,原字符串右对齐,前面填充0。
def zfill(self, width): # real signature unknown; restored from __doc__
"""
S.zfill(width) -> str
Pad a numeric string S with zeros on the left, to fill a field
of the specified width. The string S is never truncated.
"""
return ""
a = "Life is short , just use Python"
print(a.zfill(40))
# 000000000Life is short , just use Python
字符串的方法基本上全部介绍完毕, 下面一章介绍list列表的方法。