一、len(),计算字符串长度。
>>> help(len)
Help on built-in function len in module __builtin__:
len(...)
len(object) -> integer
Return the number of items of a sequence or mapping.
>>> s1 = "0123456789"
>>> len(s1)
10
二、int(),字符串转整型。
>>> s2 = int(s1[1:])
>>> print s2
123456789
>>> s2 = int(s1)
>>> print s2
123456789
>>>
三、ord(),字符串转ASCII值。
>>> ord('a')
97
>>> ord('A')
65
>>> cha(97)
四、chr(),ASCII值转字符串。
>>> chr(97)
'a'
>>> chr(65)
'A'
五、find(),查找。
>>> help(str.find)
Help on method_descriptor:
find(...)
S.find(sub [,start [,end]]) -> int
Return the lowest index in S 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.
>>> s = s1 * 5
>>> print s
01234567890123456789012345678901234567890123456789
>>> len(s)
50
>>> s.find('789012')
7
>>> s.find('789012',8)
17
六、rfind(),从右查找。
>>> help(str.rfind)
Help on method_descriptor:
rfind(...)
S.rfind(sub [,start [,end]]) -> int
Return the highest index in S 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.
>>> s.rfind('789012')
37
七、strip(),去除首尾空格;lstrip(),去除左边空格;rstrip(),去除右边空格。
>>> help(str.strip)
Help on method_descriptor:
strip(...)
S.strip([chars]) -> string or unicode
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
>>> s3 = ' 0123456789 '
>>> print s3
0123456789
>>> s = s3.strip()
>>> print s
0123456789
>>>
八、split(),字符串分割函数。
>>> help(str.split)
Help on method_descriptor:
split(...)
S.split([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, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are removed
from the result.
>>> s = 'hello the cruel world,python'
>>> print s
hello the cruel world,python
>>> lis = s.split()
>>> print lis
['hello', 'the', 'cruel', 'world,python']
>>> s = 'aa.bb.cc.com'
>>> print s
aa.bb.cc.com
>>> lis = s.split('.')
>>> print lis
['aa', 'bb', 'cc', 'com']
九、join(),join是split方法的逆方法,用来连接序列中的元素,注意被连接的序列元素都必须是字符串。
>>>
>>> s1 = ['1','2','a','d','6']
>>> s = '*'
>>> s.join(s1)
'1*2*a*d*6'
>>> print s
*
>>> print s1
['1', '2', 'a', 'd', '6']
>>> s2 = s.join(s1)
>>> print s2
1*2*a*d*6
>>>
>>> s3 = 'D:','python','test'
>>> print s3
('D:', 'python', 'test')
>>> s = '\'
SyntaxError: EOL while scanning string literal
>>> s4 = '\'
SyntaxError: EOL while scanning string literal
>>> '/'.join(s3)
'D:/python/test'
>>> print '\\'.join(s3)
D:\python\test
>>>
十、isalnum(),字母数字。
>>> help(str.isalnum)
Help on method_descriptor:
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 = 'abc1234d'
>>> s.isalnum()
True
>>> s = '/(123d'
>>> s.isalnum()
False
十一、isalpha(),纯字母。
>>> help(str.isalpha)
Help on method_descriptor:
isalpha(...)
S.isalpha() -> bool
Return True if all characters in S are alphabetic
and there is at least one character in S, False otherwise.
>>> a = 'abcd'
>>> a.isalpha()
True
>>> a = 'a12bcd'
>>> a.isalpha()
False
十二、isdigit(),纯数字。
>>> help(str.isdigit)
Help on method_descriptor:
isdigit(...)
S.isdigit() -> bool
Return True if all characters in S are digits
and there is at least one character in S, False otherwise.
>>> a = 'a12bcd'
>>> a.isdigit()
False
>>> a = '12345'
>>> a.isdigit()
True
十三、islower(),是否小写;isupper(),是否大写。
>>> s = 'ABCD'
>>> s.islower()
False
>>> s.isupper()
True
十四、isspace(),是否为空格。
>>> help(str.isspace)
Help on method_descriptor:
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 = ''
>>> s.isspace()
False
>>> s = ' '
>>> s.isspace()
True
十五、lower(),字符串全部转换成小写;upper(),字符串全部转换成大写。
>>> help(str.lower)
Help on method_descriptor:
lower(...)
S.lower() -> string
Return a copy of the string S converted to lowercase.
>>> help(str.upper)
Help on method_descriptor:
upper(...)
S.upper() -> string
Return a copy of the string S converted to uppercase.
>>> 'abcDEfG'.lower()
'abcdefg'
>>> 'abcdE'.upper()
'ABCDE'
十六、startswith(),以xx开始;endswith(),以xx结尾。
>>> help(str.startswith)
Help on method_descriptor:
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.
>>> s1 = 'www.'
>>> s2 = '.com'
>>> s3 = 'www.baidu.com'
>>> if s3.startswith(s1):
print 'yes'
yes
>>> if s3.endswith(s2):
print 'yes'
yes
十七、replace(),替换,第3个参数count表示替换的最大次数。字符串为常量,所以是值拷贝。
>>> help(str.replace)
Help on method_descriptor:
replace(...)
S.replace(old, new[, count]) -> string
Return a copy of string S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
>>> s = 'www.baidu.com www.baidu.com www.baidu.com'
>>> id(s)
24131040
>>> s = s.replace('baidu','qq',2)
>>> id(s)
20678096
>>> print s
www.qq.com www.qq.com www.baidu.com
>>> s = s.replace('baidu','qq')
>>> id(s)
24079904
>>> print s
www.qq.com www.qq.com www.qq.com
>>>