码迷,mamicode.com
首页 > 编程语言 > 详细

python文本 单独处理每个字符的方法汇总

时间:2018-01-26 14:02:26      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:osi   ons   div   tac   text   soft   font   pac   list   

python文本 单独处理字符串每个字符的方法汇总

场景:

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

方法:

1.使用liststr

  >>> a=‘abcdefg‘ 
 
>>>
list(a) 
  [
‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘

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

   
       
 
 
 
 
 
 
 
 
>>>  

2.使用for遍历字符串


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

   
       
 
 
 
 
 
 
 
 
>>>  

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

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

 
>>>  

python文本 单独处理每个字符的方法汇总

标签:osi   ons   div   tac   text   soft   font   pac   list   

原文地址:https://www.cnblogs.com/quanweiru/p/8358901.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!