As Johannes pointed out,

for c in "string":
    #do something with c

You can iterate pretty much anything in python using the for loop construct,

for example, open("file.txt") returns a file object (and opens the file), iterating over it iterates over lines in that file

with open(filename) as f:
    for line in f:
        # do something with line

If that seems like magic, well it kinda is, but the idea behind it is really simple.

There's a simple iterator protocol that can be applied to any kind of object to make the for loop work on it.

Simply implement an iterator that defines a next() method, and implement an __iter__ method on a class to make it iterable. (the __iter__ of course, should return an iterator object, that is, an object that defines next())

See official documentation

Answer from hasen on Stack Overflow
🌐
Gitbooks
buzzcoder.gitbooks.io › codecraft-python › content › string › loop-through-a-string.html
Loop through a string · CodeCraft-Python - BuzzCoder
The len() function is a Python ... · Here we show two ways to iterate over characters in a string: One way to iterate over a string is to use for i in range(len(str)):. In this loop, the variable i receives the index so that each character can be accessed using str...
Discussions

How do you make a program iterate over a string (which is already a variable) a set amount of times?
It is a bit hard to understand what you are asking. I think I am misundertanding. But here is how to iterate over a string X amount of times: iter_times = 5 my_str = "abcdefg" for i in range(iter_times): for char in my_str: print(char) If you just want the index of the character while you are iterating, you can use enumerate: my_str = "abcdefg" for i, char in enumerate(my_str): print(f"{char} has position {i} in my_str") More on reddit.com
🌐 r/learnpython
27
2
March 6, 2024
Confused about for loops in Python. Please help me break down this code.
You're right, for loops will iterate over something. It turns out that a string is one of those things. It is after all a "string" of letters. So when a string acts as an iterator, you can think of it as being a list of single characters which you loop over one at a time. More on reddit.com
🌐 r/learnpython
26
26
December 19, 2024
How to iterate over every line of a string in Python?
The title says it all. Maybe string.split('\n') and then iterate the resulting list? More on sololearn.com
🌐 sololearn.com
6
0
Python: How to iterate through a list of strings and lists?
    if type(item) is str:
        # do something with item as a string
    else:
        # do something with item as a list```
More on reddit.com
🌐 r/learnprogramming
1
0
November 24, 2021
🌐
Runestone Academy
runestone.academy › ns › books › published › fopp › Iteration › Stringsandforloops.html
7.4. Strings and for loops — Foundations of Python Programming
Since a string is simply a sequence of characters, the for loop iterates over each character automatically. (As always, try to predict what the output will be from this code before your run it.) The loop variable achar is automatically reassigned each character in the string “Go Spot Go”. ...
🌐
W3Schools
w3schools.com › python › gloss_python_for_string.asp
Python For Looping Through a String
Remove List Duplicates Reverse a String Add Two Numbers · Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... Python For Loops Tutorial For For Break For Continue Looping Through a Range For Else Nested Loops For pass
🌐
W3Schools
w3schools.com › python › python_for_loops.asp
Python For Loops
Remove List Duplicates Reverse ... Python Study Plan Python Interview Q&A Python Training ... A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string)....
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how to iterate a string in python using for loop
How to Iterate a String in Python using For Loop - Spark By {Examples}
May 21, 2024 - 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
Find elsewhere
🌐
TechBeamers
techbeamers.com › iterate-strings-python
Python Program: 5 Ways to Iterate Strings - TechBeamers
November 30, 2025 - By using the indexing method · Let’s first begin with the “for loop” method. It is the most prominent and straightforward technique to iterate strings. Follow the below sample code: """ Python Program: Using for loop to iterate over a string in Python """ string_to_iterate = "Data Science" for char in string_to_iterate: print(char)
🌐
GeeksforGeeks
geeksforgeeks.org › python › iterate-over-characters-of-a-string-in-python
Iterate over characters of a string in Python - GeeksforGeeks
July 11, 2025 - Explanation: for char in s: This line loops through the string s, with char representing each character in s one by one.
🌐
Delft Stack
delftstack.com › home › howto › python › loop over a string python
How to Loop Over a String in Python | Delft Stack
March 11, 2025 - It’s a great way to make your code more Pythonic and efficient. The enumerate() function is another excellent way to loop over a string, especially when you need to keep track of the index of each character. This function adds a counter to the iterable, allowing you to access both the index and the character simultaneously. This can be particularly useful for tasks that require both the character and its position in the string.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-for-loops
Python For Loops - GeeksforGeeks
Here, for loop is used to iterate through each character of the string and prints them one by one. ... range() function is used with for loops to generate a sequence of numbers. It can take one, two or three arguments: ... These statements are ...
Published: May 8, 2026
🌐
Dot Net Perls
dotnetperls.com › for-python
Python - for: Loop Over String Characters - Dot Net Perls
January 4, 2025 - Tip If you need to get adjacent characters, or test many indexes at once, the for-loop that uses range() is best. s = "abc" # Loop over string. for c in s: print(c) # Loop over string indexes.
🌐
LaunchCode
education.launchcode.org › lchs › chapters › loops › iterating-over-collections.html
6.5. Loop Through a String — LaunchCode's LCHS documentation
In the for statement, a string (or a variable containing a string) replaces range. The first time the loop runs, the loop variable gets assigned the first character in the string (character = 'H' or char = 'T'). Each time the loop repeats, the variable holds the next character in the string.
🌐
Codehubindia
codehubindia.in › learn › python › tutorials › python string loops
Python String Loops
Demonstrates the fundamental `for` loop structure to iterate over each character in a string and print it.Example 1 ... The code initializes `my_string` to 'Python'. The `for character in my_string:` loop assigns each character of `my_string` to the `character` variable sequentially.
🌐
Reddit
reddit.com › r/learnpython › confused about for loops in python. please help me break down this code.
r/learnpython on Reddit: Confused about for loops in Python. Please help me break down this code.
December 19, 2024 -

I am an absolute beginner in programming, so please forgive my utter lack of understanding.

I came across this concept quite recently through a course I am taking in Python. I re-watched the lessons, I have tried doing some separate research on this topic, but I am still having a hard time wrapping my head around it.

The project I am currently working on is a random password generator. I was stumped and couldn't figure it out on my own so I watched the teacher's solution as a last resort. Especially puzzled when she used this code:

password = ""
for char in password_list:
    password += char

For context, this code is supposed to convert a list of characters (from password_list) into a string.

I thought that for loops were used for iteration. I am only familiar with using "for __ in __ range(a, b)" so far, so I was quite puzzled by this loop as I don't understand how it works. Could someone please help me break it down into simpler terms? That would be super appreciated.

Please and thank you.

🌐
Java2Blog
java2blog.com › home › python › python string › loop through string in python
Loop through String in Python - Java2Blog
October 28, 2021 - The slicing operator in Python helps in accessing different parts of a sequence or string. We can use it to loop through a specific part of the string. See the code below. ... We can also use the slicing operator to iterate over a string in the reverse order. We can do this in the following way. ... The enumerate() function creates an enumerate object by adding a counter to an iterable. We can use this object in the for loop to iterate over the string by accessing its character and counter value at once.
🌐
YouTube
youtube.com › case digital
How To Iterate Through A String In Python - YouTube
In this python tutorial, I help you answer the question of how to iterate through a string in python. In fact, I give you two different methods you can use t...
Published: January 11, 2023
Views: 1K
🌐
Scientech Easy
scientecheasy.com › home › blog › iterate over string in python
Iterate over String in Python - Scientech Easy
January 26, 2026 - The parameter end = ” ” in the print() function is to place a white space after each character of a string while iterating. Thus, the the print() function ends with white space, not a newline. The for loop iterates over each character of a string and prints each letter on a separate line.
🌐
freeCodeCamp
freecodecamp.org › news › python-for-loop-example-how-to-write-loops-in-python
Python For Loop Example – How to Write Loops in Python
April 26, 2022 - For loops are useful when you want to execute the same code for each item in a given sequence. With a for loop, you can iterate over any iterable data such as lists, sets, tuples, dictionaries, ranges, and even strings. In this article, I will show you how the for loop works in Python.