python之strip()和split()

strip()和split()

strip() 方法

用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。

该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。


str1 = "0000000123456Awin_Ge3456700000000000"  #######去除首尾字符0#######
print str1.strip('0')            #######去除首尾字符0#######
输出
123456Awin_Ge34567

str2 = "  Awin_Ge   "            
print str2.strip() 
输出
Awin_Ge

str3 = "1234Awin_Ge4321"
print str3.strip('123')
输出
4Awin_Ge4

split()

通过指定分隔符对字符串进行分割并返回一个列表,默认分隔符为所有空字符,包括空格、换行(\n)、制表符(\t)等

str4 = "This is string example!!!!!!!"
print str4.split()
print str4.split('i',1)
print str4.split('!')

['This', 'is', 'string', 'example!!!!!!!']
['Th', 's is string example!!!!!!!']
['This is string example', '', '', '', '', '', '', '']
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值