1. str list 互转
def str_list():
s = "123abc"
list_tmp = list(s)
print (list_tmp)
print (''.join(list_tmp))
print ('\n')
s = "123,abc,456"
list_tmp = s.split(",")
print (list_tmp)
print (','.join(list_tmp))
结果:
['1', '2', '3', 'a', 'b', 'c']
123abc
['123', 'abc', '456']
123,abc,456
2. eval 使用
s = "['abc', '123', 'eed']"
value = eval(s)
print (type(value))
print (value)
<type 'list'>
['abc', '123', 'eed']
d = "{'abc':123, 'cbd':234}"
value = eval(d)
print (type(value))
print (value)
结果:
<type 'dict'>
{'abc': 123, 'cbd': 234}
字符串转元组
t = "(2, 'qwer', ['2wqe','abc'], 34)"
value = eval(t)
print (type(value))
print (value)
结果:
<type 'tuple'>
(2, 'qwer', ['2wqe', 'abc'], 34)
3. 字符串截取
(1) 截掉后面的字符串
s = '00E04C800628-chrome'
print (s.rstrip('-chrome'))