Python implements three ways to process one character at a time

  • 2020-04-02 14:15:18
  • OfStack

This article illustrates three ways in which python handles one character at a time. Share with you for your reference.

Specific methods are as follows:


a_string = "abccdea" 
 
print 'the first' 
for c in a_string: 
  print ord(c)+1 
 
   
print "the second"   
result = [ord(c)+1 for c in a_string] 
print result 
 
print "the thrid" 
 
def do_something(c): 
  return ord(c)+1 
 
result = map(do_something ,a_string) 
print result 

The printed result is as follows:


the first 
98 
99 
100 
100 
101 
102 
98 
the second 
[98, 99, 100, 100, 101, 102, 98] 
the thrid 
[98, 99, 100, 100, 101, 102, 98] 

I hope this article has helped you with your Python programming.


Related articles: