Python之字符串处理

1、Split()and Split(“ ”)and strip()

split() 将是按两个字符串直接的空格分割,split(" ")是按照一个空格进行分割的, strip()去除首尾空格。

>>> a = 'the sky is blue'
>>> b = ' the sky  is blue '
>>> a.split()
['the', 'sky', 'is', 'blue']
>>> b.split()
['the', 'sky', 'is', 'blue']
>>> a.split(" ")
['the', 'sky', 'is', 'blue']
>>> b.split(" ")
['', 'the', 'sky', '', 'is', 'blue', '']
>>> b.strip()
'the sky  is blue'

2、[::-1] and [:-1]

[::-1] 是将字符串中的每个字母按照逆序输出,[:-1]是截取字符串最后一个字符以前的字符串;

对于列表,逆序输出列表中的每个元素。对于列表来说reverse()函数和[::-1]等价。

>>> a = 'the sky is blue'
>>> a[::-1]
'eulb si yks eht'
>>> a[:-1]
'the sky is blu'
>>> tmp = a.split()
>>> tmp
['the', 'sky', 'is', 'blue']
>>> tmp[::-1]
['blue', 'is', 'sky', 'the']
>>>

3、“ ”.join(list)

将列表中的元素按照空格拼接输出

>>> tmp
['blue', 'is', 'sky', 'the']
>>> " ".join(tmp)
'blue is sky the'

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值