How to Iterate Through a String in Python?

In this tutorial, I will explain how to iterate through a string in Python. As a Python developer working on a project for a US-based client, I recently faced a challenge where I needed to process each character of a string individually. After experimenting with different methods, I discovered several ways to accomplish this task effectively. In this article, I will share my findings and provide detailed examples.

String Iteration

In Python, a string is a sequence of characters. When you need to process each character separately, you can iterate through the string using various techniques. Iterating over a string allows you to access each character one at a time, enabling you to perform operations like string manipulation, pattern matching, or data validation.

Read How to Remove Prefixes from Strings in Python?

Iterate Through a String in Python

Python provides several ways to iterate through a string in Python. Let us see some important methods.

Method 1. Use a For Loop

The simplest and most common way to iterate through a string in Python is by using a for loop. This method is simple and efficient. Here’s an example:

name = "John Smith"
for char in name:
    print(char)

Output:

J
o
h
n
 
S
m
i
t
h

I have executed the above example code and added the screenshot below.

Iterate Through a String in Python

In this code snippet, we have a string name with the value “John Smith”. The for loop iterates over each character in the string, and the char variable holds the value of the current character in each iteration. The print() function is used to display each character on a new line.

Read How to Convert a String to a Timestamp in Python?

Method 2. Access Characters by Index

Another way to iterate through a string is by using the index of each character. In Python, strings are zero-indexed, meaning the first character has an index of 0, the second character has an index of 1, and so on. You can use a for loop with the range() function to iterate over the indices of the string. Here’s an example:

name = "Emily Johnson"
for i in range(len(name)):
    print(name[i])

Output:

E
m
i
l
y

J
o
h
n
s
o
n

I have executed the above example code and added the screenshot below.

How to Iterate Through a String in Python

In this example, we use the len() function to get the length of the string name. The range() function generates a sequence of numbers from 0 to the length of the string minus 1. Inside the loop, we use the index i to access each character of the string using the square bracket notation name[i].

Read How to Return a String in Python?

Method 3. Use the enumerate() Function

The enumerate() function in Python allows you to iterate over a sequence (such as a string) and obtain both the index and the value of each element. This can be useful when you need to keep track of the character’s position while iterating. Here’s an example:

name = "Michael Davis"
for index, char in enumerate(name):
    print(f"Character {char} is at index {index}")

Output:

Character M is at index 0
Character i is at index 1
Character c is at index 2
Character h is at index 3
Character a is at index 4
Character e is at index 5
Character l is at index 6
Character   is at index 7
Character D is at index 8
Character a is at index 9
Character v is at index 10
Character i is at index 11
Character s is at index 12

I have executed the above example code and added the screenshot below.

Iterate Through a String in Python enumerate()

In this code, we use the enumerate() function to iterate over the characters of the string name. The index variable represents the position of each character, and the char variable holds the actual character value. We print a formatted string that displays both the character and its corresponding index.

Read How to Use a Raw Strings in Python?

Method 4. Iterate Over Substrings

Sometimes, you may need to iterate over substrings or specific parts of a string. Python provides various string slicing techniques to achieve this. Here’s an example that demonstrates iterating over words in a string:

sentence = "Sarah Anderson lives in New York"
words = sentence.split()
for word in words:
    print(word)

In this example, we have a string sentence containing a full name and a city. We use the split() method to split the string into a list of words based on whitespace. Then, we iterate over the words list using a for loop and print each word on a new line.

Read How to Sort a String in Python?

Conclusion

In this tutorial, I explained how to iterate through a string in Python. I discussed some important methods like using for loop, accessing characters by index, using the enumerate() function, iterate over substrings.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.