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

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()api

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用法相同less

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

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

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

_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)ci

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

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

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)

相關文章
相關標籤/搜索