In Python, while operating with String, one can do multiple operations on it. Let’s see how to iterate over characters of a string in Python.
Example #1: Using simple iteration and range()
# Python program to iterate over characters of a string # Code #1 string_name = "geeksforgeeks" # Iterate over the string for element in string_name: print(element, end=' ') print("\n") # Code #2 string_name = "GEEKS" # Iterate over index for element in range(0, len(string_name)): print(string_name[element]) |
g e e k s f o r g e e k s G E E K S
Example #2: Using enumerate() function
# Python program to iterate over characters of a string string_name = "Geeks" # Iterate over the string for i, v in enumerate(string_name): print(v) |
G e e k s
Example #3: Iterate characters in reverse order
# Python program to iterate over characters of a string # Code #1 string_name = "GEEKS" # slicing the string in reverse fashion for element in string_name[ : :-1]: print(element, end =' ') print('\n') # Code #2 string_name = "geeksforgeeks" # Getting length of string ran = len(string_name) # using reversed() function for element in reversed(range(0, ran)): print(string_name[element]) |
S K E E G s k e e g r o f s k e e g
Example #4: Iteration over particular set of element.
Perform iteration over string_name by passing particular string index values.
# Python program to iterate over particular set of element. string_name = "geeksforgeeks" # string_name[start:end:step] for element in string_name[0:8:1]: print(element, end =' ') |
g e e k s f o r
Recommended Posts:
- Iterate over words of a String in Python
- Iterate over a dictionary in Python
- Iterate over a list in Python
- Iterate over a set in Python
- Different ways to iterate over rows in Pandas Dataframe
- How to iterate over rows in Pandas Dataframe
- How to iterate over a JavaScript object ?
- How to iterate over the keys and values with ng-repeat in AngularJS ?
- How to iterate over filtered (ng-repeat filter) collection of objects in AngularJS ?
- How to iterate over an Array using for loop in Golang?
- Python | Iterate through value lists dictionary
- Iterate associative array using foreach loop in PHP
- How to iterate through all selected elements into an array ?
- How to iterate through child elements of a div using jQuery ?
- Python | Convert list of strings and characters to list of characters
- Python - Remove front K characters from each string in String List
- Python | Swapping sublists over given range
- Python - Constant Multiplication over List
- Replace minimal number of characters to make all characters pair wise distinct
- Check whether second string can be formed from characters of first string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

