python string模块

whitespace = ' \t\n\r\v\f'

lowercase = 'abcdefghijklmnopqrstuvwxyz'

uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

letters = lowercase + uppercase

ascii_lowercase = lowercase

ascii_uppercase = uppercase

ascii_letters = ascii_lowercase + ascii_uppercase

digits = '0123456789'

hexdigits = digits + 'abcdef' + 'ABCDEF'

octdigits = '01234567'

punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""

printable = digits + letters + punctuation + whitespace

 

capwords  (每个单词首个字母大写)

def capwords(s, sep=None):
    """capwords(s [,sep]) -> string

    Split the argument into words using split, capitalize each
    word using capitalize, and join the capitalized words using
    join.  If the optional second argument sep is absent or None,
    runs of whitespace characters are replaced by a single space
    and leading and trailing whitespace are removed, otherwise
    sep is used to split and join the words.
    """
    return (sep or ' ').join(x.capitalize() for x in s.split(sep))

swapcase (大写变小写,  小写变大写)

def swapcase(s):
    """swapcase(s) -> string

    Return a copy of the string s with upper case characters
    converted to lowercase and vice versa.

    """
    return s.swapcase()

strip (如果chars是空,去掉两端的空格,如果chars非空,去掉两端的chars )

def strip(s, chars=None):
    """strip(s [,chars]) -> string
    Return a copy of the string s with leading and trailing
    whitespace removed.
    If chars is given and not None, remove characters in chars instead.
    If chars is unicode, S will be converted to unicode before stripping.

    """
    return s.strip(chars)
lstrip和rstrip用法相同

split (s是字符串, sep是以什么分割, maxsplit是要分割几次)

def split(s, sep=None, maxsplit=-1):
    """split(s [,sep [,maxsplit]]) -> list of strings

    Return a list of the words in the string s, using sep as the
    delimiter string.  If maxsplit is given, splits at no more than
    maxsplit places (resulting in at most maxsplit+1 words).  If sep
    is not specified or is None, any whitespace string is a separator.

    (split and splitfields are synonymous)

    """
    return s.split(sep, maxsplit)

join(words是要链接的字符串或其他, sep是以什么链接,默认为空)

def join(words, sep = ' '):
    """join(list [,sep]) -> string

    Return a string composed of the words in list, with
    intervening occurrences of sep.  The default separator is a
    single space.

    (joinfields and join are synonymous)

    """
    return sep.join(words)

index (s是字符串, *arg是要查找的字符,如果没有找到将引发ValueError异常, 返回索引)

def index(s, *args):
    """index(s, sub [,start [,end]]) -> int

    Like find but raises ValueError when the substring is not found.

    """
    return s.index(*args)

count (计数, *arg出现的次数)

def count(s, *args):
    """count(s, sub[, start[,end]]) -> int

    Return the number of occurrences of substring sub in string
    s[start:end].  Optional arguments start and end are
    interpreted as in slice notation.

    """
    return s.count(*args)

_float = float

_int = int

_long = long

atof (字符数字转变为浮点型)

def atof(s):
    """atof(s) -> float

    Return the floating point number represented by the string s.

    """
    return _float(s)

atoi  (转为整数型, base是转成几进制(8,10,16))

def atoi(s , base=10):
    """atoi(s [,base]) -> int

    Return the integer represented by the string s in the given
    base, which defaults to 10.  The string s must consist of one
    or more digits, possibly preceded by a sign.  If base is 0, it
    is chosen from the leading characters of s, 0 for octal, 0x or
    0X for hexadecimal.  If base is 16, a preceding 0x or 0X is
    accepted.

    """
    return _int(s, base)

atol  (转为长整形)

def atol(s, base=10):
    """atol(s [,base]) -> long

    Return the long integer represented by the string s in the
    given base, which defaults to 10.  The string s must consist
    of one or more digits, possibly preceded by a sign.  If base
    is 0, it is chosen from the leading characters of s, 0 for
    octal, 0x or 0X for hexadecimal.  If base is 16, a preceding
    0x or 0X is accepted.  A trailing L or l is not accepted,
    unless base is 0.

    """
    return _long(s, base)

ljust, rjust, center (为了输出美观)

print '|','*'.ljust(10),'|'
print '|','*'.ljust(10,'-'),'|'
print '|','*'.rjust(10,'-'),'|'
print '|','*'.center(10,'-'),'|'

| *          |
| *--------- |
| ---------* |

| ----*----- |

repace替换

def replace(s, old, new, maxsplit=-1):
    """replace (str, old, new[, maxsplit]) -> string

    Return a copy of string str with all occurrences of substring
    old replaced by new. If the optional argument maxsplit is
    given, only the first maxsplit occurrences are replaced.

    """
    return s.replace(old, new, maxsplit)



 

你可能感兴趣的:(python string模块)