python中字符串类型的一些学习总结

本文详细解析了Python中的字符串类型,包括内置类型str的功能和用法,以及Lib/string.py中的string类。对比了str与string.ascii_letters等特性,并介绍了字符串格式化的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

刷题时遇到了string类的常量string.ascii_letters,调用时发现,string竟然不是python的built-in type??

然后就打开了官方文档,发现还是自己菜了。原来python中自带的字符串类型是str啊!并不是string。string是在The Python Standard Library里的(Lib/string.py),需要用 import string 语句导入的。因为平常用字符串就直接用引号双引号定义了,于是就忽略了这些。再加上字符串相关的题也挺多的,今天就来整理一下python中这两个字符串类型吧。先从自带类型str开始。

Text Sequence Type — str

String literals are written in a variety of ways:

  • Single quotes: 'allows embedded "double" quotes'
  • Double quotes: "allows embedded 'single' quotes".
  • Triple quoted: '''Three single quotes'''"""Three double quotes"""

Triple quoted strings may span multiple lines - all associated whitespace will be included in the string literal.

Strings may also be created from other objects using the  str  constructor.

Strings implement all of the common sequence operations, 

OperationResultNotes
x in sTrue if an item of s is equal to x, else False(1)
x not in sFalse if an item of s is equal to x, else True(1)
s + tthe concatenation of s and t(6)(7)
s * n or n * sequivalent to adding s to itself n times(2)(7)
s[i]ith item of s, origin 0(3)
s[i:j]slice of s from i to j(3)(4)
s[i:j:k]slice of s from i to j with step k(3)(5)
len(s)length of s 
min(s)smallest item of s 
max(s)largest item of s 
s.index(x[, i[, j]])index of the first occurrence of x in s (at or after index i and before index j)(8)
s.count(x)total number of occurrences of x in s 

along with the additional methods described below.(摘抄几个主要的,具体的参见python doc  https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range)

str. capitalize ( )

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

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.

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).

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.


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

Return an encoded version of the string as a bytes object.

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.

str. lower ( )

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

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 endare  interpreted as in slice notation. Return -1 if sub is not found.

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.

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

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

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

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

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.

>>>
>>> "The sum of 1 + 2 is {0}".format(1+2)
'The sum of 1 + 2 is 3'
str. islower ( )

Return true if all cased characters [4] in the string are lowercase and there is at least one cased character, false otherwise.

str. isspace ( )

Return true if there are only whitespace characters in the string and there is at least one character, false otherwise. Whitespace characters are those characters defined in the Unicode character database as “Other” or “Separator” and those with bidirectional property being one of “WS”, “B”, or “S”.

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.

str. isupper ( )

Return true if all cased characters [4] in the string are uppercase and there is at least one cased character, false otherwise.

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. 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 initerable, including bytes objects. The separator between elements is the string providing this method.

       

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.


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.

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.

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.

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:

>>>
>>> '   spacious   '.strip()
'spacious'
>>> 'www.example.com'.strip('cmowz.')
'example'
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.

str. title ( )

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

For example:

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

然后是格式化的输出 String Formatting

具体见文档,这里举几个例子就大概可以看明白了。

The conversion types are:

ConversionMeaningNotes
'd'Signed integer decimal. 
'i'Signed integer decimal. 
'o'Signed octal value.(1)
'u'Obsolete type – it is identical to 'd'.(6)
'x'Signed hexadecimal (lowercase).(2)
'X'Signed hexadecimal (uppercase).(2)
'e'Floating point exponential format (lowercase).(3)
'E'Floating point exponential format (uppercase).(3)
'f'Floating point decimal format.(3)
'F'Floating point decimal format.(3)
'g'Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise.(4)
'G'Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise.(4)
'c'Single character (accepts integer or single character string). 
'r'String (converts any Python object using repr()).(5)
's'String (converts any Python object using str()).(5)
'a'String (converts any Python object using ascii()).(5)
'%'No argument is converted, results in a '%' character in the result. 


built-in type的str说完了,再来看看Lib/string.py 里的string类。

https://docs.python.org/3/library/string.html

里面有更多str的格式输出的例子。

看了看感觉没必要摘抄了。直接看doc吧~




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值