最新消息:380元/半年,推荐全网最具性价比的一站式编程学习平台码丁实验室

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

Python scratch2010 1539浏览 0评论

友情提示:380元/半年,儿童学编程,就上码丁实验室

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

场景:

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

方法:

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']  
  >>>   

您必须 登录 才能发表评论!