Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Python text is a summary of how each character is processed individually


May 10, 2021 Python2


Table of contents


python text A summary of the methods that handle each character of a string individually

Scene:

The string is processed one character at a time

Method:

1. Use 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. Use for to traverse the string


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

3. Use for to parse the string into the list

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