接字符串的切片
字符串的判断子串
判断字符 | 字符串是否属于字符串,用 字符/字符串 in 字符串
In [5]: a = 'hello world'
In [6]: 'a' in a ##判断字符a是否属于字符串a
Out[6]: False
In [7]: 'ello' in a ##判断字符串ello是否属于字符串a
Out[7]: True
In [8]: 'elo' in a
Out[8]: False
In [9]: 'w' in a
Out[9]: True
字符串的重复、连接以及计算长度
重复:字符串*(n) ##n为重复的次数
连接:字符串+字符串
计算长度:len(字符串)
In [1]: print 'hello '*10
hello hello hello hello hello hello hello hello hello hello
In [2]: print 'hello'+'world'
helloworld
In [3]: a = 'hello westos@@'
In [4]: len(a)
Out[4]: 14
字符串常用操作
字符串的操作有:
In [5]: a
Out[5]: 'hello westos@@'
In [6]: a.
a.capitalize a.format a.isupper a.rindex a.strip
a.center a.index a.join a.rjust a.swapcase
a.count a.isalnum a.ljust a.rpartition a.title
a.decode a.isalpha a.lower a.rsplit a.translate
a.encode a.isdigit a.lstrip a.rstrip a.upper
a.endswith a.islower a.partition a.split a.zfill
a.expandtabs a.isspace a.replace a.splitlines
a.find a.istitle a.rfind a.startswith
可以使用help()查看字符串的帮助
In [6]: help(a.format) ##查看帮助
Help on built-in function format: ##帮助
format(...)
S.format(*args, **kwargs) -> string
Return a formatted version of S, using substitutions from args and kwargs.
The substitutions are identified by braces ('{' and '}').
(END)
常用:
a.isalnum ##判断是否是字母或数字
a.isalpha ##判断是否是字母
a.isdigit ##判断是否是数字
a.islower ##判断是否是小写字母
a.isupper ##判断是否是大写字母
a.isspace ##判断是否是英文空格
a.istitle ##判断是否是标题(开头字母为大小写)