python的split用法

Python中没有字符类型的说法,只有字符串,这里所说的字符就是只包含一个字符的字符串!!!
这里这样写的原因只是为了方便理解,仅此而已。

1. 按照某一个分隔符分割一个字符串:

>>> str = "my name is liu de hua"

>>> str

'my name is liu de hua'

>>> split_str = str.split(' ')

>>> print split_str

['my', 'name', 'is', 'liu', 'de', 'hua']

>>> 

 

2. 按照某一个分隔符分割“多少次”:

>>> str 

'my name is liu de hua'

>>> split_str2 = str.split(' ', 2)

>>> split_str2

['my', 'name', 'is liu de hua']

>>> 

 

3. 按某一字符(或字符串)分割,且分割n次,并将分割的完成的字符串(或字符)赋给新的(n+1)个变量。如:按‘.’分割字符,且分割1次,并将分割后的字符串赋给2个变量str1,str2

>>> url = "www.google.com"

>>> str1, str2 = url.split('.',1)

>>> str1

'www'

>>> str2

'google.com'

>>> 

 

你可能感兴趣的:(python)