Iterate over Characters of a String in Python

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)
Copy

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.

Related Tutorials

Code copied to clipboard successfully 👍