How to iterate through a string in Python?

By Lenin Mishra

If you prefer to watch Youtube videos over reading blogs, check out our video on Python strings here.

Looping through a string

We can iterate or loop through a string in Python by using the for loop.

Code

str1 = "Pylenin"

for letter in str1:
    print(letter)

Output

P
y
l
e
n
i
n
  1. How to create a string in Python?
  2. How to access characters in a Python string?
  3. How to replace characters in a string in Python?
  4. How to concatenate strings in Python?
  5. Check if a Substring is Present in a Given String in Python
  6. Escape sequences in Python String
  7. Python String Formatting - The Definitive Guide
  8. Commonly used Python string methods