您的位置:首页 > 编程语言 > Python开发

轻松python文本专题-单独处理字符串每个字符的方法汇总

2015-08-25 07:00 721 查看
场景:

用每次处理一个字符的方式处理字符串

方法:

1.使用list(str)

>>> a='abcdefg'
>>> list(a)
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> aList=list(a)
>>> for item in aList:
print(item)#这里可以加入其他的操作,我们这里只是单纯使用print

a
b
c
d
e
f
g
>>>


2.使用for遍历字符串

>>> a='abcdefg'
>>> for item in a :
print(item)#这里可以加入其他的操作,我们这里只是单纯使用print

a
b
c
d
e
f
g
>>>


3.使用for解析字符串到list里面

>>> a='abcdefg'
>>> result=[item for item in a]
>>> result
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>>


就说到这里,谢谢大家

------------------------------------------------------------------

点击跳转零基础学python-目录
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: