python之字符串

来源:互联网 发布:ipv6有什么好处知乎 编辑:程序博客网 时间:2024/04/28 09:40

接字符串的切片

字符串的判断子串

判断字符 | 字符串是否属于字符串,用 字符/字符串 in 字符串

In [5]: a = 'hello world'In [6]: 'a' in a    ##判断字符a是否属于字符串aOut[6]: FalseIn [7]: 'ello' in a ##判断字符串ello是否属于字符串aOut[7]: TrueIn [8]: 'elo' in aOut[8]: FalseIn [9]: 'w' in aOut[9]: True

字符串的重复、连接以及计算长度

重复:字符串*(n) ##n为重复的次数
连接:字符串+字符串
计算长度:len(字符串)

In [1]: print 'hello '*10hello hello hello hello hello hello hello hello hello hello In [2]: print 'hello'+'world'helloworldIn [3]: a = 'hello westos@@'In [4]: len(a)Out[4]: 14

字符串常用操作

字符串的操作有:

In [5]: aOut[5]: 'hello westos@@'In [6]: a.a.capitalize  a.format      a.isupper     a.rindex      a.stripa.center      a.index       a.join        a.rjust       a.swapcasea.count       a.isalnum     a.ljust       a.rpartition  a.titlea.decode      a.isalpha     a.lower       a.rsplit      a.translatea.encode      a.isdigit     a.lstrip      a.rstrip      a.uppera.endswith    a.islower     a.partition   a.split       a.zfilla.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           ##判断是否是标题(开头字母为大小写)
原创粉丝点击