字符串分割(split 方法)

帮助:

>>> help(str.split)
Help on method_descriptor:
 
split(...)
    S.split([sep[, maxsplit]]) -> list of strings
    
    Return a list of the words in S, using sep as the
    delimiter string.  If maxsplit is given, at most maxsplit
    splits are done. If sep is not specified or is None, any
    whitespace string is a separator and empty strings are
    removed from the result.

测试代码:

>>> a_str = '1#2#3#4#5#6#7'
>>> a_str.split('#')
['1', '2', '3', '4', '5', '6', '7']
>>> a_str.split('#', 1)
['1', '2#3#4#5#6#7']
>>> a_str.split('#', 2)
['1', '2', '3#4#5#6#7']
>>> a_str.split('#', 3)
['1', '2', '3', '4#5#6#7']
>>> a_str.split('#', 4)
['1', '2', '3', '4', '5#6#7']
>>> a_str.split('#', 5)
['1', '2', '3', '4', '5', '6#7']
>>> a_str.split('#', 6)
['1', '2', '3', '4', '5', '6', '7']

参数说明:

sep:分隔符,表示从字符串什么地方对其进行分隔;

maxsplit:表示把字符串分割到哪个分隔符(从字符串最左边开始,第一个分隔符的位置是1)指示的位置为止,这里 maxsplit 指示的那个位置是最后一个要分隔的地方。

注:

如果空参数调用 split,则分隔符是任何空白字符(whitespace,空格,Tab,或多个的联合)。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值