python string/list转换

python的read、write方法的操作对象都是string。输入、输出和逻辑业务上很多时候都要用到string、list互转。

1.简单用法

import string
str = 'abcde'
list = list(str)
print list
# ['a', 'b', 'c', 'd', 'e']
str_convert = ''.join(list)
print str_convert
# 'abcde'

2.使用split()将一个字符串分裂成多个字符串组成的列表。

str2 = "abc grt werwe"
list2 = str2.split() # or list2 = str2.split(" ")
print list2
# ['abc', 'grt', 'werwe']

str3 = "www.google.com"
list3 = str3.split(".")
print list3
# ['www', 'google', 'com']

3.使用 strip() 方法移除字符串头尾指定的字符。

ids = '1001,1002,1003,1004,'
ids_list = ids.strip(',').split(',')
print ids_list
# ['1001', '1002', '1003', '1004']

4.使用自定字符连接list中的字符串组成一个新字符串

list3 = ['www', 'google', 'com']
str4 = "".join(list3)
print str4
# wwwgooglecom
str5 = ".".join(list3)
print str5
# www.google.com
str6 = " ".join(list3)
print str6
# www google com

 

done!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值