1. 程式人生 > >python中字串型別的一些學習總結

python中字串型別的一些學習總結

刷題時遇到了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吧~