RUI个人博客 首页>>Python>>[原]Python 字符串string相关 built-in function

[原]Python 字符串string相关 built-in function

[原]Python 字符串string相关 built-in function

本文是自己学习过程的一个整理的笔记,或许有一些不恰当的地方,如有遇到可以指出,大家一同讨论。

本文是对于上一篇文章的一个补充部分:

Python 字符串string模块详解,string中所有的内建方法
http://www.5ibig.com/article/index/arcid/61.htm

由于上篇文章中是对于 string 模块的详解,但是其中有较多的内建方法在 python 2.7.10 中已经变成了内建方法,

所有本文是对上一文章的一个补充:


序列类型操作符
序列操作符 作用
seq [ind] 获得下表为ind的元素
seq [ind1:ind2] 获得下标从 ind1 到 ind2 间的元素集合
seq * expr 序列重复 expr 次
seq1 + seq2 连接序列 seq1 和 seq2
obj in seq 判断 obj 元素是否包含在 seq 中
obj not in seq 判断 obj 元素是否不包含在 seq 中

字符串类型内建方法:
string.ascii_letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
string.lowercase 'abcdefghijklmnopqrstuvwxyz'
string.uppercase 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
string.digits '0123456789'
string.hexdigits(十六进制) '0123456789abcdefABCDEF'
string.letters 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
string.octdigits(八进制) '01234567'
string.printable '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQR
STUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
string.punctuation(标点符号) '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
string.whitespace(空白) '\t\n\x0b\x0c\r '
4. s.decode(encoding='UTF-8' errors='strict')
    将其他编码的字符换转换成 unicode 编码。以dcoding 指定的编码格式解码 string,如果出错默认报一个 ValueError 的异常,除非 errors 指定的       是'ignore'或者'replace'
Python释义(2.7.10里已成内建函数)
decode(...)
    S.decode([encoding[,errors]]) -> object
   
    Decodes S using the codec registered for encoding. encoding defaults
    to the default encoding. errors may be given to set a different error
    handling scheme. Default is 'strict' meaning that encoding errors raise
    a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
    as well as any other name registered with codecs.register_error that is
    able to handle UnicodeDecodeErrors.
示例:

>>> s.decode()
u'I am tyger,I love Python'
>>> s.decode('gb18030')
u'I am tyger,I love Python'

5. string.encode(encoding='UTF-8' errors='strict')

    将 unicode 编码转换成其他编码的字符串。以encoding 指定的编码格式编码 string,如果出错默认报一个 ValueError 的异常,除非 errors 指定的     是'ignore'或者'replace'
Python释义(2.7.10里已成内建函数)
encode(...)
    S.encode([encoding[,errors]]) -> object
   
    Encodes S using the codec registered for encoding. encoding defaults
    to the default encoding. errors may be given to set a different error
    handling scheme. Default is 'strict' meaning that encoding errors raise
    a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
    'xmlcharrefreplace' as well as any other name registered with
    codecs.register_error that is able to handle UnicodeEncodeErrors.
示例:

>>> s.encode()
'I am tyger,I love Python'
>>> s.encode('gb2312')
'I am tyger,I love Python'
>>> s.encode('gb18030')
'I am tyger,I love Python'

6. string.endswith(obj,beg=0,end=len(string))

    检查 str 是否包含在 string 中,如果 beg 和 end 指定范围,则检查是否包含在指定范围内,
    如果是返回开始的索引值,则返回 -1
Python释义(2.7.10里已成内建函数)
endswith(...)
    S.endswith(suffix[, start[, end]]) -> bool
   
    Return True if S ends with the specified suffix, False otherwise.
    With optional start, test S beginning at that position.
    With optional end, stop comparing S at that position.
    suffix can also be a tuple of strings to try.
示例:

>>> s
'I am tyger,I love Python'
>>> s.endswith('on')
True
>>> s.endswith('yon')
False
>>> s.startswith('yon')
False
>>> s.startswith('Py')
False
>>> s.startswith('I ')
True

10. string.isalnum()

    如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False
Python释义(2.7.10里已成内建函数)
isalnum(...)
    S.isalnum() -> bool
   
    Return True if all characters in S are alphanumeric
    and there is at least one character in S, False otherwise.
示例:

>>> s
'I am tyger,I love Python'
>>> s2
'I am 24,I like []='
>>> s3
'HelloWorld'
>>> s4
'66668888'
>>> s.isalnum()
False
>>> s2.isalnum()
False
>>> s3.isalnum()
True
>>> s4.isalnum()
True

11. string.isalpha()

    如果 string 至少有一个字符并且所有的字符都是字母则返回 True,否则返回 False
Python释义(2.7.10里已成内建函数)
isalpha(...)
    S.isalpha() -> bool
   
    Return True if all characters in S are alphabetic
    and there is at least one character in S, False otherwise.
示例:

>>> s
'I am tyger,I love Python'
>>> s2
'I am 24,I like []='
>>> s3
'HelloWorld'
>>> s4
'66668888'
>>> s.isalpha()
False
>>> s2.isalpha()
False
>>> s3.isalpha()
True
>>> s4.isalpha()
False

12. string.isdecimal()

注:仅对 Unicode 字符串有效。如果 string 只包含十进制数字则返回 True,否则返回 False
Python释义(2.7.10里已成内建函数)
isdecimal(...)
    S.isdecimal() -> bool
   
    Return True if there are only decimal characters in S,
    False otherwise.
示例:

>>> s
'I am tyger,I love Python'
>>> s2
'I am 24,I like []='
>>> s3
'HelloWorld'
>>> s4
'66668888'
>>> s3u = s3.decode()
>>> s3u.isdecimal()
False
>>> s4u = s4.decode()
>>> s4u.isdecimal()
True
13. string.isdigit()
    如果 string 只包含数字则返回 True,否则返回 False
Python释义(2.7.10里已成内建函数)
isdigit(...)
    S.isdigit() -> bool
   
    Return True if all characters in S are digits
    and there is at least one character in S, False otherwise.
示例:

>>> s
'I am tyger,I love Python'
>>> s2
'I am 24,I like []='
>>> s3
'HelloWorld'
>>> s4
'66668888'
>>> s.isdigit()
False
>>> s2.isdigit()
False
>>> s3.isdigit()
False
>>> s4.isdigit()
True

14. string.islower()

    如果 string 中包含至少一个区分大小写的字符,
    并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False
Python释义(2.7.10里已成内建函数)
islower(...)
    S.islower() -> bool
   
    Return True if all cased characters in S are lowercase and there is
    at least one cased character in S, False otherwise.
示例:

>>> s3
'HelloWorld'
>>> s4
'66668888'
>>> s5
'helloworld'
>>> s6
'66668888tyger'
>>> s3.islower()
False
>>> s4.islower()
False
>>> s5.islower()
True
>>> s6.islower()
True

15. string.isnumeric()

注:仅对 Unicode 字符串有效。如果 string 中只包含数字字符,则返回 True,否则返回 False
Python释义(2.7.10里已成内建函数)
isnumeric(...)
    S.isnumeric() -> bool
   
    Return True if there are only numeric characters in S,
    False otherwise.
示例:

>>> s3u
u'HelloWorld'
>>> s4u
u'66668888'
>>> s3u.isnumeric()
False
>>> s4u.isnumeric()
True

16. string.isspace()

    如果 string 中只包含空格,则返回 True,否则返回 False
Python释义(2.7.10里已成内建函数)
isspace(...)
    S.isspace() -> bool
   
    Return True if all characters in S are whitespace
    and there is at least one character in S, False otherwise.
示例:

>>> s
'I am tyger,I love Python'
>>> s2
'I am 24,I like []='
>>> s3
'HelloWorld'
>>> s4
'66668888'
>>> s.isspace()
False
>>> s2.isspace()
False
>>> s3.isspace()
False
>>> s4.isspace()
False
>>> s8='I '
>>> s8.isspace()
False
>>> s8=' '
>>> s8.isspace()
True

17. string.istitle()

    如果 string 是标题化的(见 title()) 则返回 True,否则返回 False
Python释义(2.7.10里已成内建函数)
istitle(...)
    S.istitle() -> bool
   
    Return True if S is a titlecased string and there is at least one
    character in S, i.e. uppercase characters may only follow uncased
    characters and lowercase characters only cased ones. Return False
    otherwise.
示例:

>>> s3
'HelloWorld'
>>> s5
'helloworld'
>>> s3.istitle()
False
>>> s5.istitle()
False
>>> s9='Hello'
>>> s10='hello'
>>> s9.istitle()
True
>>> s10.istitle()
False

18. string.isupper()

    如果 string 中包含至少一个区分大小写的字符,
    并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False
Python释义(2.7.10里已成内建函数)
isupper(...)
    S.isupper() -> bool
   
    Return True if all cased characters in S are uppercase and there is
    at least one cased character in S, False otherwise.
示例:

>>> s3
'HelloWorld'
>>> s3u
u'HelloWorld'
>>> s5
'helloworld'
>>> s3.isupper()
False
>>> s3u.isupper()
False
>>> s5.isupper()
False
>>> s13='HELLOWORLD'
>>> s13.isupper()
True
>>> s13.decode().isupper()
True

23. string.partition(str) and string.rpartition(str)

    有点像 find() 或 split() 的结合体,从 str 出现的第一个位置起,把字符串 string 分成
    一个 3 元组(string_pre_str,str,string_post_str),如果 string 中不包含 str 则 string_pre_str == string
Python释义(2.7.10里已成内建函数)
partition(...)
    S.partition(sep) -> (head, sep, tail)
   
    Search for the separator sep in S, and return the part before it,
    the separator itself, and the part after it. If the separator is not
    found, return S and two empty strings.
示例:

>>> s
'I am tyger,I love Python'
>>> s.partition(',')
('I am tyger', ',', 'I love Python')
>>> 
>>> s.partition('I')
('', 'I', ' am tyger,I love Python')
>>> s.partition('haha')
('I am tyger,I love Python', '', '')
>>> s.partition()  
Traceback (most recent call last):
  File "", line 1, inTypeError: partition() takes exactly one argument (0 given)
>>> s.partition('')
Traceback (most recent call last):
  File "", line 1, inValueError: empty separator

>>> s.partition('I')
('', 'I', ' am tyger,I love Python')
>>> s.rpartition('I')
('I am tyger,', 'I', ' love Python')

28. string.rpartition(str)

    类似于 partition()函数,不过是从右边开始查找
Python释义(2.7.10里已成内建函数)
rpartition(...)
    S.rpartition(sep) -> (head, sep, tail)
   
    Search for the separator sep in S, starting at the end of S, and return
    the part before it, the separator itself, and the part after it. If the
    separator is not found, return two empty strings and S.
示例:

>>> s.partition('I')
('', 'I', ' am tyger,I love Python')
>>> s.rpartition('I')
('I am tyger,', 'I', ' love Python')

32. string.splitlines(num=string.count('\n'))

    按照行分隔,返回一个包含各行作为元素的列表,则仅分隔 num 个子字符串
Python释义(2.7.10里已成内建函数)
splitlines(...)
    S.splitlines(keepends=False) -> list of strings
   
    Return a list of the lines in S, breaking at line boundaries.
    Line breaks are not included in the resulting list unless keepends
    is given and true.
示例:

>>> s
'I am tyger,I love Python'
>>> s.splitlines()
['I am tyger,I love Python']
>>> ssl='''
... this is 1 line
... this is 2 line
... this is 3 line
... '''
>>> 
>>> 
>>> ssl
'\nthis is 1 line\nthis is 2 line\nthis is 3 line\n'
>>> ssl.splitlines()
['', 'this is 1 line', 'this is 2 line', 'this is 3 line']

33. string.startswith(obj,beg=0,end=len(string))

    检查字符串是否是以 obj 开头,是则返回 True,否则返回 False,如果 beg 和 end 指定值,则在指定范围内检查
Python释义(2.7.10里已成内建函数)
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.
示例:

>>> s
'I am tyger,I love Python'
>>> s.endswith('on')
True
>>> s.endswith('yon')
False
>>> s.startswith('yon')
False
>>> s.startswith('Py')
False
>>> s.startswith('I ')
True

36. string.title()

    返回"标题化"的 string,就是说所有单词都是以大写开始,其余字母均为小写(见istitle())
Python释义(2.7.10里已成内建函数)
title(...)
    S.title() -> string
   
    Return a titlecased version of S, i.e. words start with uppercase
    characters, all remaining cased characters have lowercase.

示例:

>>> s9
'Hello'
>>> s10
'hello'
>>> s11
'heLLo'
>>> 
>>> s9.title()
'Hello'
>>> s10.title()
'Hello'
>>> s11.title()
'Hello'
>>> s11.title().istitle()
True
2016.12.02 补充上传于 北京邮储传    tyger.wang
昵  称:
邮  箱:
评论内容:
验 证 码:
可用[code][/code]插入代码
点击刷新验证码