How to iterate a string in Python using For loop? You can iterate over each character of a string using multiple ways of Python. The string is a basic datatype of Python that contains a collection of characters within a single or double quotes. In this article, I will explain while iterating a string using for loop, how we can get each character of the given string in multiple ways. And also explain using reversed() function how we can iterate over the string and get each character in reverse order.
1. Quick Examples of Iterating String using For Loop
Following are quick examples of how to iterate string using for loop.
# Quick Examples of iterate string using for loop.
# Example 1: Iterate over characters of a string
# using for loop
str = “SparkByExample”
print(“String is:”, str)
print(“Characters of string:”)
for char in str:
print(char)
# Example 2: Iterate over the string by index
print(“Characters of string:”)
for i in range(0, len(str)):
print(i, str[i])
# Example 3: Iterate over the string by index using enumerate()
print(“Characters of string:”)
for i, char in enumerate(str):
print(i, char)
# Example 4: Iterate over characters of a string in reverse order
print(“Characters of string in reverse order:”)
for i in reversed(range(0, len(str))):
print(str[i], end = ‘ ‘)
print(“n”)
# Example 5: Iterate over the selected character of the string
print(“Characters of string:”)
for char in str[0:7]:
print(char, end = ‘ ‘)
print(“n”)
# Example 6: Iterate over the selected character of the string
print(“Selected Characters of string:”)
for char in itertools.islice(str, 0, 7):
print(char, end = ‘ ‘)
print(“n”)
What is String in Python?
A string in Python is a data type and data structure that is used to store sequences of characters and manipulate text-based data. They are represented by a set of characters enclosed in quotes, either single or double.
Python String datatype support indexing, which allows you to access individual characters within a string. This can be useful for tasks such as parsing and extracting information from strings.
2. Iterate Over a String using Python For Loop
You can use for loop to iterate over a string and get each character of the string. Let’s take a string and iterate it using for loop, for every iteration to get each character presented in the string.
# Iterate over characters of a string
# using for loop
str = “SparkByExample”
print(“String is:”, str)
print(“Characters of string:”)
for char in str:
print(char)
Yields below output.
3. Iterate String using For Loop by Index
While iterating over the string using for loop, you can get the corresponding index of each character of a given string. You can use the Python range() function to get the range of the sequence and use the len() function to get the length of the Passed object. You can iterate range(0, len(str)) using for loop, for every iteration to get the index and corresponding character of the string.
str = “SparkBy”
print(“String is:”, str)
# Iterate over the string by index
print(“Characters of string:”)
for i in range(0, len(str)):
print(i, str[i])
Yields below output.
Alternatively, iterate over a string by index using enumerate() function. Python enumerate() function is a built-in function and is used to iterate a sequence and return the index of each item presented in the sequence. You can iterate enumerate(str) using for loop, for every iteration to get the index and corresponding character of the string.
# Iterate over the string by index using enumerate()
print(“Characters of string:”)
for i, char in enumerate(str):
print(i, char)
Yields below output.
String is: SparkBy
Characters of string:
0 S
1 p
2 a
3 r
4 k
5 B
6 y
4. Iterate String using Python For Loop in Reverse Order
Alternatively, you can use for loop to iterate over the string and get each character in reverse order. You can pass range(0, len(str)) into reversed() function and iterate it using for loop, for each iteration you can retrieve the index of the string in reverse order. You can use the retrieved index to access the corresponding character of a given string.
# Iterate over characters of a string in reverse order
str = “SparkByExample”
print(“String is:”, str)
print(“Characters of string in reverse order:”)
for i in reversed(range(0, len(str))):
print(str[i], end = ‘ ‘)
print(“n”)
# Output:
# String is: SparkByExample
# Characters of string in reverse order:
# e l p m a x E y B k r a p S
5. Iterate Selected Portion of String using For Loop
You can use slicing technique to iterate over the selected portion of string. Let’s use slicing to iterate over selected portion of string. For example,
# Iterate over the selected character of string
print(“Characters of string:”)
for char in str[0:7]:
print(char, end = ‘ ‘)
print(“n”)
# Output:
# String is: SparkByExample
# Characters of string:
# S p a r k B y
Alternatively, you can use the itertools module to iterate over the string and get the selected part of the string in Python.
# Iterate over the selected character of string
import itertools
str = “SparkByExample”
print(“String is:”, str)
print(“Selected Characters of string:”)
for char in itertools.islice(str, 0, 7):
print(char, end = ‘ ‘)
print(“n”)
# Output:
# String is: SparkByExample
# Selected Characters of string:
# S p a r k B y
6. Iterate Python String using while loop
A while loop in Python will iterate over a given sequence of elements until a given condition is met. In this case, we will loop through a string until the given condition is met.
str = “SparkBy”
print(“String is:”, str)
# Iterate over the string using while loop
print(“Characters of string:”)
i = 0
while i < len(str):
print(str[i])
i += 1
# Output:
# String is: SparkBy
# Characters of string:
# S
# p
# a
# r
# k
# B
# y
7. Conclusion
In this article, I have explained using for loop over a string and how we can get each character of the given string in multiple ways. And explained using reversed() function how we can iterate over the string in reverse order.
Happy Learning!!
How to iterate a string in Python using For loop? You can iterate over each character of a string using multiple ways of Python. The string is a basic datatype of Python that contains a collection of characters within a single or double quotes. In this article, I will explain while iterating a string using Read More Python, Python Tutorial, Pending review by admin, Python For Loop Examples, Python String Examples