python-split()/join()/strip() 方法

1 字符串的spilt() 方法

    str.split(str="", num=string.count(str))

str – 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
num – 分割次数。
返回是一个列表

str = "this is string example....wow!!!"
print (str.split( ))
print (str.split('i',1))
print (str.split('w'))

输出:

['this', 'is', 'string', 'example....wow!!!']
['th', 's is string example....wow!!!']
['this is string example....', 'o', '!!!']

2 字符串的join() 方法

    str.join(sequence)

sequence – 要连接的元素序列。

s1 = "-"
s2 = ""
seq = ("r", "u", "n", "o", "o", "b") # 字符串序列
print (s1.join( seq ))
print (s2.join( seq ))

输出:

r-u-n-o-o-b
runoob

3 字符串的strip() 方法

    str.strip([chars]);

先看无参的例子:

mystr = ' \nabc def 123 abc \t'
newstr = mystr.strip()
print(newstr)
#输出
abc def 123 abc

简单来说就是去除字符串前后的空字符,包括空格、换行(\n)、制表符(\t)等,返回一个新的字符串.

mystr = 'abcdef123abc'
newstr = mystr.strip('abc')
print(newstr)
#输出
def123

当有参数的时候,函数会首先将参数分隔成一个个的字符,然后在字符串收尾去掉这些字符,直到遇到第一个不是参数的字符.

还有lstrip() rstrip() 分别是只对字符串首 尾进行删除.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值