python 数据类型-字符串-对象和方法

python的字符串有众多方法,可以在doc文档中查看

示例

转换开头字母为大写

c1="welcome to my python"
>>> c1.capitalize()
'Welcome to my python'
>>> print(c1)
welcome to my python
>>> c2=c1.capitalize()
>>> print(c2)
Welcome to my python

统计出现次数

>>> c1.count('o')
3

find查询

>>> hw='hello world'
>>> hw
'hello world'
>>> hw.find('world')
6
>>> hw[6:]
'world'

split分割

>>> ip1="192.168.1.123"
>>> ip2="192.189.2.33"
>>> ip1.split('.')
['192', '168', '1', '123']

分割

ip1.split('.')[-1]
'123'

连接

>>> ip1=ip1.split('.')
>>> ip1
['192', '168', '1', '123']
>>> ".".join(ip1)
'192.168.1.123'
>>> "-------".join(ip1)

 



posted @ 2017-10-27 18:53  缘琪梦  阅读(311)  评论(0编辑  收藏  举报