Google News
logo
Python Program to Loop through a string
In the following example of Python program that demonstrates how to loop through a string :
Program :
# Looping through a string
text = "hello"

for char in text:
    print(char)
Output :
h
e
l
l
o
In this program, we use a `for` loop to iterate over each character in the string `text`. Inside the loop, we use the `print()` function to print each character on a new line.

You can use a `for` loop to iterate over each character in a string or any other sequence of items such as a list or tuple. The loop will execute once for each item in the sequence, and the loop variable (in this case, `char`) will take on the value of each item in turn.