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

时间:2022-07-24 21:42:38

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('.')
['', '', '', '']

分割

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

连接

>>> ip1=ip1.split('.')
>>> ip1
['', '', '', '']
>>> ".".join(ip1)
'192.168.1.123'
>>> "-------".join(ip1)