python3.12 Class str详解

字符串作为计算里面一个重要的对象,在Python的实现是通过定义类来描述它的。这是一篇字典式的文章,我们详细描述字符串实例的各个办法。
一:
实例化的办法:‘’。比如:

s='python'

二:字符串类的办法。(参数列表中的 “ / ",表示在此之前的都是位置参数。
1.
capitalize
2.casefold
3.center
4.count
5.encode
6.endswith
7.expandtabs
8.find
9. 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 ‘}’).
|
10. format_map(…)
| S.format_map(mapping) -> str
|
| Return a formatted version of S, using substitutions from mapping.
| The substitutions are identified by braces (‘{’ and ‘}’).

  1. index(…)
    | 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.
    11.1
    返回发现的匹配的子串的第一个字母的切片位置
>>> s='abca'
>>> s.index('a')
0

发现不了,会报错,正如文档的说明。

  1. isalnum(self, /)
    | Return True if the string is an alpha-numeric string, False otherwise.
    |
    | A string is alpha-numeric if all characters in the string are alpha-numeric and
    | there is at least one character in the string.
    |
  2. isalpha(self, /)
    | Return True if the string is an alphabetic string, False otherwise.
    |
    | A string is alphabetic if all characters in the string are alphabetic and there
    | is at least one character in the string.
    |
  3. isascii(self, /)
    | Return True if all characters in the string are ASCII, False otherwise.
    |
    | ASCII characters have code points in the range U+0000-U+007F.
    | Empty string is ASCII too.
    |
  4. isdecimal(self, /)
    | Return True if the string is a decimal string, False otherwise.
    |
    | A string is a decimal string if all characters in the string are decimal and
    | there is at least one character in the string.

16.isdecimal(self, /)
| Return True if the string is a decimal string, False otherwise.
|
| A string is a decimal string if all characters in the string are decimal and
| there is at least one character in the string.
|

17 isdigit(self, /)
| Return True if the string is a digit string, False otherwise.
|
| A string is a digit string if all characters in the string are digits and there
| is at least one character in the string.
|

18 isidentifier(self, /)
| Return True if the string is a valid Python identifier, False otherwise.
|
| Call keyword.iskeyword(s) to test whether string s is a reserved identifier,
| such as “def” or “class”.
|
19.islower(self, /)
| Return True if the string is a lowercase string, False otherwise.
|
| A string is lowercase if all cased characters in the string are lowercase and
| there is at least one cased character in the string.
|
20. isnumeric(self, /)
| Return True if the string is a numeric string, False otherwise.
|
| A string is numeric if all characters in the string are numeric and there is at
| least one character in the string.
|

  1. isprintable(self, /)
    | Return True if the string is printable, False otherwise.
    |
    | A string is printable if all of its characters are considered printable in
    | repr() or if it is empty.
    |

22 isspace(self, /)
| Return True if the string is a whitespace string, False otherwise.
|
| A string is whitespace if all characters in the string are whitespace and there
| is at least one character in the string.
|

  1. istitle(self, /)
    | Return True if the string is a title-cased string, False otherwise.
    |
    | In a title-cased string, upper- and title-case characters may only
    | follow uncased characters and lowercase characters only cased ones.
    |

  2. isupper(self, /)
    | Return True if the string is an uppercase string, False otherwise.
    |
    | A string is uppercase if all cased characters in the string are uppercase and
    | there is at least one cased character in the string.

  3. join(self, iterable, /)
    | Concatenate any number of strings.
    |
    | The string whose method is called is inserted in between each given string.
    | The result is returned as a new string.
    |
    | Example: ‘.’.join([‘ab’, ‘pq’, ‘rs’]) -> ‘ab.pq.rs’
    25.1按照这个函数的说明,下面就是我们常用的,把列表转为字符串的办法(用空字符串)

''.join( L)

比如:

l=[1,2,3]
s =''.join( l)

会报错,必须要字符列表的实例😅

TypeError: sequence item 0: expected str instance, int found

25.2换成字符串列表,如下(或者考虑int 转str)

>>> l=['fly','me','to','the','moon']
>>> l
['fly', 'me', 'to', 'the', 'moon']
>>> ''.join(l)
'flymetothemoon'
>>>

25.3
还可以在字符串列表中,增添我们想要的东西:

>>> li =['a','new','moon','is','raising']
>>> li
['a', 'new', 'moon', 'is', 'raising']
>>> 'xxx'.join( li)
'axxxnewxxxmoonxxxisxxxraising'
>>> '**'.join( li)
'a**new**moon**is**raising'

25.4
另外,int如何转为str呢?只要使用str关键字就好了,但是str转为int也许应该小心,如下:
int转str

>>> type( 1)
<class 'int'>
>>> s = str(1)
>>> type( s)
<class 'str'>
>>> s
'1'

str转int,报错。因为编译器会预先检查整个字符串是否可以转为十进制。(就是按照十进制的在当前编译环境下的编码(一般是utf-8),去查?)尚不清楚原理?

>>> s ='moon'
>>> type(s)
<class 'str'>
>>> i =int( s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'moon'

26 ljust(self, width, fillchar=’ ', /)
| Return a left-justified string of length width.
|
| Padding is done using the specified fill character (default is a space).
|
|

27.lower(self, /)
| Return a copy of the string converted to lowercase.

  1. lstrip(self, chars=None, /)yiju
    | Return a copy of the string with leading whitespace removed.
    |
    | If chars is given and not None, remove characters in chars instead.
    |

  2. partition(self, sep, /)
    | Partition the string into three parts using the given separator.
    |
    | This will search for the separator in the string. If the separator is found,
    | returns a 3-tuple containing the part before the separator, the separator
    | itself, and the part after it.
    |
    | If the separator is not found, returns a 3-tuple containing the original string
    | and two empty strings.

  3. removeprefix(self, prefix, /)
    | Return a str with the given prefix string removed if present.
    |
    | If the string starts with the prefix string, return string[len(prefix):].
    | Otherwise, return a copy of the original string.

  4. removesuffix(self, suffix, /)
    | Return a str with the given suffix string removed if present.
    |
    | 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.

  5. replace(self, old, new, count=-1, /)
    | Return a copy with all occurrences of substring old replaced by new.
    |
    | count
    | Maximum number of occurrences to replace.
    | -1 (the default value) means replace all occurrences.
    |
    | If the optional argument count is given, only the first count occurrences are replaced.

  6. rfind(…)
    | 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.
    |

  7. rindex(…)
    | 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.

  8. rjust(self, width, fillchar=’ ', /)
    | Return a right-justified string of length width.
    |
    | Padding is done using the specified fill character (default is a space).

  9. rpartition(self, sep, /)
    | Partition the string into three parts using the given separator.
    |
    | This will search for the separator in the string, starting at the end. If
    | the separator is found, returns a 3-tuple containing the part before the
    | separator, the separator itself, and the part after it.
    |
    | If the separator is not found, returns a 3-tuple containing two empty strings
    | and the original string.

  10. rsplit(self, /, sep=None, maxsplit=-1)
    | Return a list of the substrings in the string, using sep as the separator string.
    |
    | sep
    | The separator used to split the string.
    |
    | When set to None (the default value), will split on any whitespace
    | character (including \n \r \t \f and spaces) and will discard
    | empty strings from the result.
    | maxsplit
    | Maximum number of splits.
    | -1 (the default value) means no limit.
    |
    | Splitting starts at the end of the string and works to the front.

  11. rstrip(self, chars=None, /)
    | Return a copy of the string with trailing whitespace removed.
    |
    | If chars is given and not None, remove characters in chars instead.

  12. split(self, /, sep=None, maxsplit=-1)
    | Return a list of the substrings in the string, using sep as the separator string.
    |
    | sep
    | The separator used to split the string.
    | When set to None (the default value), will split on any whitespace character (including \n \r \t \f and spaces) and will discard empty strings from the result.
    | maxsplit
    | Maximum number of splits.
    | -1 (the default value) means no limit.
    |
    | Splitting starts at the front of the string and works to the end.
    |
    | Note, str.split() is mainly useful for data that has been intentionally delimited. With natural text that includes punctuation, consider using the regular expression module.
    39.1没有指定分隔依据的符号的时候,将使用whitespace作为分割依据。所谓的whitespace包括包括了 \n \r \t \f,在字符串中的表现分别是 换行 回车 制表 \f暂时不清楚,并且还会自动帮你去掉空字符串。非常nice。

>>> s = 'fly me to the moon'
>>> s.split()
['fly', 'me', 'to', 'the', 'moon']
>>> s = ' fly  me  to  the  moon  '
>>> s.split()
['fly', 'me', 'to', 'the', 'moon']

测试制表、换行、回车

>>> s=' fly\t me\n to\r the\f moon'
>>> s
' fly\t me\n to\r the\x0c moon'
>>> print(s)
 fly     me
 the moon
>>> print('\f')

>>> s.split()
['fly', 'me', 'to', 'the', 'moon']
>>>

请注意,上面的两段话是不一样的。我们显示地写出来

fly空格me空格to空格the空格moon
空格空格fly空格空格me空格空格to空格空格the空格空格moon空格空格

当不传入参数的时候,实际上去掉了所有whitespace,并按每个whitespace作为分割标志去分割字符。
39.2最大分割符出现次数,默认是没有限制的,当然指定参数的情况下,会左边开始,优先分割

>>> s='everyting flows, here comes another new day'
>>> s.split( maxsplit=2)
['everyting', 'flows,', 'here comes another new day']
>>> s.split( maxsplit=3)
['everyting', 'flows,', 'here', 'comes another new day']
>>>

39.3注意到函数参数说明的“/”,说明self是位置参数,表明了调用函数的对象本身必须当作位置参数传入。之后的 sep和maxsplit都是默认参数。
我们不需要写出默认参数的名字,直接传入就可以了,注意哈,self自动传入,不占用位置,但是不想指明sep,只想指明maxsplit就可以如上调用,否则:

>>> s.split(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str or None, not int

39.4我们当然可以指定分割符,它只能是一个字符串。
选择” “ 为句子的分割依据的时候,因为句子没有‘’所以会直接返回整个列表
选择’e’作为分割符号(依据)的时候,会把e直接变成’, '两个单引号,递归的走完整个字符串。

>>> s.split('*')
['everyting flows, here comes another new day']
>>> s.split('e')
['', 'v', 'ryting flows, h', 'r', ' com', 's anoth', 'r n', 'w day']
  1. splitlines(self, /, keepends=False)
    | Return a list of the lines in the string, breaking at line boundaries.
    |
    | Line breaks are not included in the resulting list unless keepends is given and true.

  2. startswith(…)
    | 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.

  3. strip(self, chars=None, /)
    | Return a copy of the string with leading and trailing whitespace removed.
    |
    | If chars is given and not None, remove characters in chars instead.

  4. swapcase(self, /)
    | Convert uppercase characters to lowercase and lowercase characters to uppercase.

  5. title(self, /)
    | Return a version of the string where each word is titlecased.
    |
    | More specifically, words start with uppercased characters and all remaining
    | cased characters have lower case.

  6. translate(self, table, /)
    | Replace each character in the string using the given translation table.
    |
    | table
    | Translation table, which must be a mapping of Unicode ordinals to
    | Unicode ordinals, strings, or None.
    |
    | The table must implement lookup/indexing via getitem, for instance a
    | dictionary or list. If this operation raises LookupError, the character is
    | left untouched. Characters mapped to None are deleted.

  7. upper(self, /)
    | Return a copy of the string converted to uppercase.

  8. zfill(self, width, /)
    | Pad a numeric string with zeros on the left, to fill a field of the given width.
    |
    | The string is never truncated.
    |
    | ----------------------------------------------------------------------

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值