Iterate over Characters of a String


Iterate over Characters of a String

To iterate over the characters of a string in Python, use For loop statement. During each iteration of the for loop, we get to access each character of the string.

Syntax

The syntax to access each character ch of the string s is

for ch in s:
  print(ch)

Example

In this example, we will read a string into variable s, and iterate over the characters using for-in loop.

Python Program

x = input('enter a string: ')

for ch in x:
    print(ch)

Output

enter a string: hello
h
e
l
l
o

Summary

In this tutorial of Python Examples, we learned how to iterate over the characters of a given string using For Loop, with the help of well detailed examples.