python 之 string

因为用 python 主要还是集中在字符处理等上,所以有必要加深学习, 今天首先学习一下 string 的一些 方法,虽然也有 string 这个 module ,但是内置的 string.method 应该也挺丰富的,很多东西是重合的。并且由于 python 3.4 目前已经默认支持中文编码,而且很多新的特性,所以主要以 3x 为主学习, 目前python 还是新手,入门水平。

文档来源: python string

String Method :

str.capitalize()

Return a copy of the string with its first character capitalized and the rest lowercased. 对于一个字符串, 第一个字符为大写, 其余为小写。

str.center(width[, fillchar])

Return centered in a string of length width.

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

Return the number of non-overlapping occurrences of substring sub in the range [start, end]. 可以只是一个字符或者一个字符pattern, 也可以使用 str.count(str[1:4]) 这种切片 count 的方式。

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

Return True if the string ends with the specified suffix, otherwise return False. 主要就是判断字符串是否是以某一后缀结尾, 可以只是一个字符或者一个字符pattern, 也可以使用 str.endswith(str[1:4]) 这种切片的方式。

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

Return the lowest index in the string where substring sub is found, such that sub is contained in the slice s[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found. Note: The find() method should be used only if you need to know the position of sub. To check if sub is a substring or not, use the in operator:

    >>> 'Py' in 'Python'
    True

str.format(*args, **kwargs)

Perform a string formatting operation. 应该和 原来的 % 差不多。
参考文档:文档

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

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

str.isalnum()

Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise. 应该就是判断字符是不是都是字母或者数字。

str.isalpha()

Return true if all characters in the string are alphabetic and there is at least one character, false otherwise. 判断字符是不是都是字母。

str.islower()

Return true if all cased characters in the string are lowercase and there is at least one cased character, false otherwise. 判断字符是不是都为小写。

str.isupper()

Return true if all cased characters in the string are uppercase and there is at least one cased character, false otherwise. 判断字符是不是都为大写。

str.join(iterable)

Return a string which is the concatenation of the strings in the iterable iterable.

    >>>a = 'abcd'
    >>>b = '123'
    >>>a.join(b)
    '1abcd2abcd3'

str.lower()

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

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 argument , 那就把左边的空白字符去掉.

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

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.

>>> a = 'abcdfde'
>>> a.partition('d')
('abc', 'd', 'fde')

str.replace(old, new[, 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.

>>> a = 'abcdfde'
>>> a.replace('d', 'D')
'abcDfDe'
>>> a = 'abcdfde'
>>> a.replace('d', 'D', 1)
'abcDfde'

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.

>>> a = 'abababab'
>>> a.rfind('b')
7
>>> a.rfind(a[0:2])
6
>>> a.rfind('m')
-1

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

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

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.

>>> a = 'abababab'
>>> a.rpartition('a')
('ababab', 'a', 'b')
>>> a.rpartition('l')
('', '', 'abababab')

str.rsplit(sep=None, maxsplit=-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()

>>> a = 'a:b:c:d:e'
>>> a.rsplit(':')
['a', 'b', 'c', 'd', 'e']
>>> a.rsplit(sep=':')
['a', 'b', 'c', 'd', 'e']
>>> a.rsplit(sep=':', maxsplit=-1)
['a', 'b', 'c', 'd', 'e']
>>> a.rsplit(sep=':', maxsplit=1)
['a:b:c:d', 'e']

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

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

str.split(sep=None, maxsplit=-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).
If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). The sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']). Splitting an empty string with a specified separator returns ['']

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

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns []

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

str.splitlines([keepends])

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.
This method splits on the following line boundaries.

Representation 	Description
\n 	Line Feed
\r 	Carriage Return
\r\n 	Carriage Return + Line Feed
\v or \x0b 	Line Tabulation
\f or \x0c 	Form Feed
\x1c 	File Separator
\x1d 	Group Separator
\x1e 	Record Separator
\x85 	Next Line (C1 Control Code)
\u2028 	Line Separator
\u2029 	Paragraph Separator

>>> 'ab c\n\nde fg\rkl\r\n'.splitlines()
['ab c', '', 'de fg', 'kl']
>>> 'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)
['ab c\n', '\n', 'de fg\r', 'kl\r\n']

Unlike split() when a delimiter string sep is given, this method returns an empty list for the empty string, and a terminal line break does not result in an extra line:

>>> "".splitlines()
[]
>>> "One line\n".splitlines()
['One line']
>>> ''.split('\n')
['']
>>> 'Two lines\n'.split('\n')
['Two lines', '']

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

Return True if string starts with the prefix, otherwise return False. prefix 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.

>>> a = 'abcdefgh'
>>> a.startswith('a')
True
>>> a.startswith('abc')
True

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

>>> a = 'abcDEF'
>>> a.swapcase()
'ABCdef'

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'

str.translate(table)

Return a copy of the string in which each character has been mapped through the given translation table. You can use str.maketrans() to create a translation map from character-to-character mappings in different formats.

reference doc

str.upper()

Return a copy of the string with all the cased characters [4] converted to uppercase. Note that str.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).

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

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

posted on 2015-09-08 19:51  OA_maque  阅读(360)  评论(0编辑  收藏  举报

导航