#python学习笔记(六)#最全的string method示例

目录

str.capitalize()

str.casefold()

str.center(width[, fillchar])

str.count(sub[, start[, end]])

str.encode(encoding='utf-8', errors='strict')

str.endswith(suffix[, start[, end]])

str.expandtabs(tabsize=8)

str.find(sub[, start[, end]])

str.format(*args, **kwargs)

str.index(sub[, start[, end]])¶

str.isalnum()

str.isalpha()

str.isascii()

str.isdecimal()

str.isdigit()

str.isidentifier()

str.islower()

str.isnumeric()

str.isprintable()

str.isspace()

str.istitle()

str.isupper()

str.join(iterable)

str.ljust(width[, fillchar])

str.lower()

str.lstrip([chars])

str.partition(sep)

str.removeprefix(prefix, /)

str.removesuffix(suffix, /)

str.replace(old, new[, count])

str.rfind(sub[, start[, end]])¶

str.rindex(sub[, start[, end]])

str.rjust(width[, fillchar])

str.rpartition(sep)

str.rsplit(sep=None, maxsplit=- 1)

str.rstrip([chars])

str.split(sep=None, maxsplit=- 1)

str.startswith(prefix[, start[, end]])

str.strip([chars])

str.swapcase()

str.title()

str.upper()

str.zfill(width)

总结


str.capitalize()

Return a copy of the string with its first character capitalized and the rest lowercased.

返回首字母大写的字符串

>>> 'banana'.capitalize()
'Banana'

str.casefold()

Return a casefolded copy of the string. Casefolded strings may be used for caseless matching.

Casefolding is similar to lowercasing but more aggressive because it is intended to remove all case distinctions in a string. For example, the German lowercase letter 'ß' is equivalent to "ss". Since it is already lowercase, lower() would do nothing to 'ß'casefold() converts it to "ss".

比lower()更严格的小写字符串

>>> 'BaNaNA'.casefold()
'banana'

str.center(width[, fillchar])

Return centered in a string of length width. Padding is done using the specified fillchar (default is an ASCII space). The original string is returned if width is less than or equal to len(s).

将字符串放一堆相同的字母的中心,width为放入后的字符总宽度,fillchar为字母,默认为空格

>>> 'banana'.center(9)
'  banana '
>>> 'banana'.center(9,'c')
'ccbananac'

str.count(sub[, start[, end]])

Return the number of non-overlapping occurrences of substring sub in the range [startend]. Optional arguments start and end are interpreted as in slice notation.

 计数字符串中某字符的个数,备选参数选择某一段字符进行计数

>>> 'bananananana'.count('na')
5
>>> 'bananananana'.count('na',5)
3
>>> 'bananananana'.count('na',5,9)
1

str.encode(encoding='utf-8'errors='strict')

Return an encoded version of the string as a bytes object. Default encoding is 'utf-8'errors may be given to set a different error handling scheme. The default for errors is 'strict', meaning that encoding errors raise a UnicodeError. Other possible values are 'ignore''replace''xmlcharrefreplace''backslashreplace' and any other name registered via codecs.register_error(), see section Error Handlers. For a list of possible encodings, see section Standard Encodings.

By default, the errors argument is not checked for best performances, but only used at the first encoding error. Enable the Python Development Mode, or use a debug build to check errors.

Changed in version 3.1: Support for keyword arguments added.

Changed in version 3.9: The errors is now checked in development mode and in debug mode.

返回字符串的字节编码版本

>>> '易烊千玺'.encode()
b'\xe6\x98\x93\xe7\x83\x8a\xe5\x8d\x83\xe7\x8e\xba'

str.endswith(suffix[, start[, end]])

Return True if the string ends with the specified suffix, otherwise return Falsesuffix can also be a tuple of suffixes to look for. With optional start, test beginning at that position. With optional end, stop comparing at that position.

判断字符是否以某字段结尾,start和end可以指定开始和结束判断的地方

>>> 'banana'.endswith('na')
True
>>> 'banana'.endswith('na',3,4)
False

str.expandtabs(tabsize=8)

Return a copy of the string where all tab characters are replaced by one or more spaces, depending on the current column and the given tab size. Tab positions occur every tabsize characters (default is 8, giving tab positions at columns 0, 8, 16 and so on). 

将字符串中的\t变成空格,空格长度由tabsize指定,默认为8

>>> 'b\ta\tn\ta\tn\ta'.expandtabs()
'b       a       n       a       n       a'
>>> 'b\ta\tn\ta\tn\ta'.expandtabs(0)
'banana'

str.find(sub[, start[, end]])

Return the lowest index in the string where substring sub is found within the slice s[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.

在字符串中查找某字符,start和end表示查找区间,返回找到的位置,没找到返回-1

>>> 'banana'.find('na')
2
>>> 'banana'.find('na',3)
4
>>> 'banana'.find('za')
-1

str.format(*args**kwargs)

Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces {}. Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument.

替换文本中{}内容,{}中数字表示format参数中的位置,以0开始

>>> 'The sum of 1+2 is {0},{1} is the sum of 2+2'.format(1+2,2+2)
'The sum of 1+2 is 3,4 is the sum of 2+2'

str.index(sub[, start[, end]])

Like find(), but raise ValueError when the substring is not found.

和find一样,但是没找到时会报错

>>> 'banana'.index('na')
2
>>> 'banana'.index('za')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found

str.isalnum()

Return True if all characters in the string are alphanumeric and there is at least one character, False otherwise. A character c is alphanumeric if one of the following returns Truec.isalpha()c.isdecimal()c.isdigit(), or c.isnumeric().

判断字符串是否时字母数字组成,并且至少包含一个字符

>>> '123banana'.isalnum()
True
>>> '123*banana'.isalnum()
False
>>> ''.isalnum()
False

str.isalpha()

Return True if all characters in the string are alphabetic and there is at least one character, False otherwise. Alphabetic characters are those characters defined in the Unicode character database as “Letter”, i.e., those with general category property being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”. Note that this is different from the “Alphabetic” property defined in the Unicode Standard.

判断是否是字母组成,且至少包含一个字符

>>> 'banana'.isalpha()
True
>>> '123banana'.isalpha()
False
>>> ''.isalpha()
False

str.isascii()

Return True if the string is empty or all characters in the string are ASCII, False otherwise. ASCII characters have code points in the range U+0000-U+007F.

判断字符串是否是空或ASCII

>>> ''.isascii()
True
>>> 'ba123nana*'.isascii()
True
>>> '易烊千玺'.isascii()
False

str.isdecimal()

Return True if all characters in the string are decimal characters and there is at least one character, False otherwise. Decimal characters are those that can be used to form numbers in base 10, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. Formally a decimal character is a character in the Unicode General Category “Nd”.

判断是否为十进制数,为空false

>>> '123'.isdecimal()
True
>>> '1X2'.isdecimal()
False
>>>

str.isdigit()

Return True if all characters in the string are digits and there is at least one character, False otherwise. Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits. This covers digits which cannot be used to form numbers in base 10, like the Kharosthi numbers. Formally, a digit is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal.

判断是否是数字

>>> '123A'.isdigit()
False
>>> '123'.isdigit()
True

str.isidentifier()

Return True if the string is a valid identifier according to the language definition, section Identifiers and keywords.

判断是否为非法字符(reserved word)

>>> 'class'.isidentifier()
True
>>> 'abc'.isidentifier()
True

str.islower()

Return True if all cased characters 4 in the string are lowercase and there is at least one cased character, False otherwise.

判断是否为小写字母

>>> 'banana123'.islower()
True
>>> 'BaNana'.islower()
False

str.isnumeric()

Return True if all characters in the string are numeric characters, and there is at least one character, False otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric.

判断是否为数字

str.isprintable()

Return True if all characters in the string are printable or the string is empty, False otherwise. Nonprintable characters are those characters defined in the Unicode character database as “Other” or “Separator”, excepting the ASCII space (0x20) which is considered printable. (Note that printable characters in this context are those which should not be escaped when repr() is invoked on a string. It has no bearing on the handling of strings written to sys.stdout or sys.stderr.)、

判断是否字符串中所有内容可以打印,空为ture

>>> '123\t'.isprintable()
False
>>> '123'.isprintable()
True
>>> ''.isprintable()
True

str.isspace()

Return True if there are only whitespace characters in the string and there is at least one character, False otherwise.

判断是否都是空格,为空时时false 

>>> '    '.isspace()
True
>>> '     \t'.isspace()
True
>>> '   \n'.isspace()
True
>>> '    123 \n'.isspace()
False
>>> ''.isspace()
False

str.istitle()

Return True if the string is a titlecased string and there is at least one character, for example uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return False otherwise.

判断第一个字母是否大写,如果以数字开头(不区分大小写)则判断跟在后面的第一个字母

>>> '123Banan'.istitle()
True
>>> '123baNana'.istitle()
False

str.isupper()

Return True if all cased characters 4 in the string are uppercase and there is at least one cased character, False otherwise.

判断是否全部大写

>>>'BANANA'.isupper()
True
>>>'banana'.isupper()
False
>>>'baNana'.isupper()
False
>>>' '.isupper()
False

str.join(iterable)

Return a string which is the concatenation of the strings in iterable. A TypeError will be raised if there are any non-string values in iterable, including bytes objects. The separator between elements is the string providing this method.

将字符串加到iterable中间,每个字符中间插一次

>>> 'banana'.join('123')
'1banana2banana3'
>>> 'N'.join('baaa')
'bNaNaNa'

str.ljust(width[, fillchar])

Return the string left justified in a string of length width. Padding is done using the specified fillchar (default is an ASCII space). The original string is returned if width is less than or equal to len(s).

字符串左对齐,width为总字符长度,大于str长度时fillchar(单个字符)填充,默认为空格填充;小于str长度打印完整字符

>>> 'banana'.ljust(9)
'banana   '
>>> 'banana'.ljust(9,'A')
'bananaAAA'
>>> 'banana'.ljust(2)
'banana'

str.lower()

Return a copy of the string with all the cased characters 4 converted to lowercase.

返回小写字符

>>> 'BaNana'.lower()
'banana'

str.lstrip([chars])

Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its values are stripped

从左边删除chars内包含的字符,删到第一个非chars内的字符为止,若chars为空则删除空格

注意和removeprefix的区别,后者删除字段

>>> '   spacious   '.lstrip()
'spacious   '
>>> 'www.example.com'.lstrip('cmowz.')
'example.com

>>> 'Arthur: three!'.lstrip('Arthur: ')
'ee!'
>>> 'Arthur: three!'.removeprefix('Arthur: ')
'three!

str.partition(sep)

Split the string at the first occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings.

根据sep分隔字符串,返回tuple包含sep前的内容,sep和sep后的内容

>>> 'www.example.com'.partition('.')
('www', '.', 'example.com')
>>> 'banana'.partition('.')
('banana', '', '')

str.removeprefix(prefix/)

If the string starts with the prefix string, return string[len(prefix):]. Otherwise, return a copy of the original string

如果字符串以prefix开头,则删除prefix字段,否则输出完整字符串

>>> 'TestHook'.removeprefix('Test')
'Hook'
>>> 'BaseTestCase'.removeprefix('Test')
'BaseTestCase'

str.removesuffix(suffix/)

If the string ends with the suffix string and that suffix is not empty, return string[:-len(suffix)]. Otherwise, return a copy of the original string

删除字符串后缀suffix

>>> 'MiscTests'.removesuffix('Tests')
'Misc'
>>> 'TmpDirMixin'.removesuffix('Tests')
'TmpDirMixin

str.replace(oldnew[, count])

Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

字符串的old字段替换成new字段,count指定替换前几个

>>> 'bananananana'.replace('na','he')
'bahehehehehe'
>>> 'bananananana'.replace('na','he',3)
'bahehehenana'

str.rfind(sub[, start[, end]])

Return the highest index in the string 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.

从右边开始找某字段,没找到返回-1

>>> 'banananan'.rfind('na')
6

str.rindex(sub[, start[, end]])

Like rfind() but raises ValueError when the substring sub is not found.

同rfind,没找到时报错

>>> 'banananan'.rindex('za')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found

str.rjust(width[, fillchar])

Return the string right justified in a string of length width. Padding is done using the specified fillchar (default is an ASCII space). The original string is returned if width is less than or equal to len(s).

 右对齐,字符总宽度width,填充字符fillchar,默认为空格

>>> 'banana'.rjust(9)
'   banana'
>>> 'banana'.rjust(9,'A')
'AAAbanana'
>>> 'banana'.rjust(3)
'banana'

str.rpartition(sep)

Split the string at the last occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself.

从右开始按sep分隔,返回包含三个元素的tuple

>>> 'www.example.com'.rpartition('.')
('www.example', '.', 'com')

str.rsplit(sep=Nonemaxsplit=- 1)

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done, the rightmost ones. If sep is not specified or None, any whitespace string is a separator. Except for splitting from the right, rsplit() behaves like split() which is described in detail below.

从右开始按sep分隔,返回一个列表,如果sep是None,把空格字符当作分隔界定,maxsplit定义最大分隔数

>>> 'hello, world\tand\n Jackson'.rsplit()
['hello,', 'world', 'and', 'Jackson']
>>> 'hello, world\tand\n Jackson'.rsplit(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str or None, not int
>>> 'hello, world\tand\n Jackson'.rsplit(maxsplit=2)
['hello, world', 'and', 'Jackson']
>>> 'www.example.com.cn'.rsplit('.')
['www', 'example', 'com', 'cn']
>>> 'www.example.com.cn'.rsplit('.',2)
['www.example', 'com', 'cn']

str.rstrip([chars])

Return a copy of the string with trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a suffix; rather, all combinations of its values are stripped:

 从右边删除chars指定字符,删到第一个非指定的字符为止,chars为空则删除空格

注意和removesuffix的区别

>>> '   spacious   '.rstrip()
'   spacious'
>>> 'mississippi'.rstrip('ipz')
'mississ'

>>> 'Monty Python'.rstrip(' Python')
'M'
>>> 'Monty Python'.removesuffix(' Python')
'Monty'

str.split(sep=Nonemaxsplit=- 1)

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).

 从左开始按sep分隔,返回一个列表,如果sep是None,把空格字符当作分隔界定,maxsplit定义最大分隔数

>>> '1,2,3'.split(',')
['1', '2', '3']
>>> '1,2,3'.split(',', maxsplit=1)
['1', '2,3']
>>> '1,2,,3,'.split(',')
['1', '2', '', '3', '']

>>> '1 2 3'.split()
['1', '2', '3']
>>> '1 2 3'.split(maxsplit=1)
['1', '2 3']
>>> '   1   2   3   '.split()
['1', '2', '3']

str.startswith(prefix[, start[, end]])

Return True if string starts with the prefix, otherwise return Falseprefix can also be a tuple of prefixes to look for. With optional start, test string beginning at that position. With optional end, stop comparing string at that position.

判断字符串是否以某字段开始,start和end确定判断区间

>>> 'bananan'.startswith('ba')
True
>>> 'bananan'.startswith('ba',4,7)
False

str.strip([chars])

Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped

从左右两边删除chars中包含的字符,删到第一个不包含在chars内的停止,chars空时删除空格

>>> '   spacious   '.strip()
'spacious'
>>> 'www.example.com'.strip('cmowz.')
'example'

>>> comment_string = '#....... Section 3.2.1 Issue #32 .......'
>>> comment_string.strip('.#! ')
'Section 3.2.1 Issue #32'

str.swapcase()

Return a copy of the string with uppercase characters converted to lowercase and vice versa. Note that it is not necessarily true that s.swapcase().swapcase() == s.

 大小写互换 

>>> 'BanaNa'.swapcase()
'bANAnA'

str.title()

Return a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase.

每个单词首字母大写

>>> 'Hello world'.title()
'Hello World'

>>> "they're bill's friends from the UK".title()
"They'Re Bill'S Friends From The Uk"


###更人性化的转化
>>> import re
>>> def titlecase(s):
...     return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
...                   lambda mo: mo.group(0).capitalize(),
...                   s)
...
>>> titlecase("they're bill's friends.")
"They're Bill's Friends."

str.upper()

Return a copy of the string with all the cased characters 4 converted to uppercase. Note that s.upper().isupper() might be False if s contains uncased characters or if the Unicode category of the resulting character(s) is not “Lu” (Letter, uppercase), but e.g. “Lt” (Letter, titlecase).

字母大写

>>> 'banana'.upper()
'BANANA'

str.zfill(width)

Return a copy of the string left filled with ASCII '0' digits to make a string of length width. A leading sign prefix ('+'/'-') is handled by inserting the padding after the sign character rather than before. The original string is returned if width is less than or equal to len(s).

在字符左边填充0,使字符长度达到width,能够识别+/-号并把它们置于最前面

>>> "42".zfill(5)
'00042'
>>> "-42".zfill(5)
'-0042'

总结

▲字母大小写类:

  str.capitalize()、str.casefold()、str.lower()、str.swapcase()、str.title()、str.upper()

▲字符填充类:

  str.center(width[, fillchar])、str.join(iterable)、str.ljust(width[, fillchar])、str.rjust(width[, fillchar])、str.zfill(width)

▲查找计数类:

  str.count(sub[, start[, end]])str.endswith(suffix[, start[, end]])str.find(sub[, start[, end]])str.index(sub[, start[, end]])、str.replace(oldnew[, count])str.rfind(sub[, start[, end]])、str.rindex(sub[, start[, end]])、str.startswith(prefix[, start[, end]])

▲字段删除类:

  str.expandtabs(tabsize=8)、str.lstrip([chars]、str.removeprefix(prefix/)、str.removesuffix(suffix/)、str.rstrip([chars])str.strip([chars])

▲字符拆分类:

  str.partition(sep)str.rpartition(sep)、str.rsplit(sep=Nonemaxsplit=- 1)、str.split(sep=Nonemaxsplit=- 1)

▲判断类型类:

  str.isalnum()、str.isalpha()、str.isascii()、str.isdecimal()、str.isdigit()、str.isidentifier()、str.islower()、str.isnumeric()、str.isprintable()、str.isspace()、str.istitle()、str.isupper()

▲其他:

  str.format(*args**kwargs)

更详细的字符串方法教程

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值