Question #346   Submitted by Answiki on 12/31/2020 at 06:18:53 PM UTC

How to iterate over string with index in Python?

Answer   Submitted by Answiki on 12/31/2020 at 06:21:22 PM UTC

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 Answiki on 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 Answiki 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.