Question #568

How to iterate over string with index in Python?

Answer

The best way to iterate over string with index is to convert the string to an enumerate object:

>>> str = "Hello!"
>>> for index, char in enumerate(str):
>>>  print (index, ' => ' , char)
0  =>  H
1  =>  e
2  =>  l
3  =>  l
4  =>  o
5  =>  !

2 events in history
Answer by Alphonsio 12/31/2020 at 06:21:22 PM

The best way to iterate over string with index is to convert the string to an enumerate object:

>>> str = "Hello!"
>>> for index, char in enumerate(str):
>>>  print (index, ' => ' , char)
0  =>  H
1  =>  e
2  =>  l
3  =>  l
4  =>  o
5  =>  !

Question by Alphonsio 12/31/2020 at 06:18:53 PM
How to iterate over string with index in Python?
# ID Query URL Count

Icons proudly provided by Friconix.