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
🌐
TechBeamers
techbeamers.com › iterate-strings-python
Python Program: 5 Ways to Iterate Strings - TechBeamers
November 30, 2025 - Another quite simple way to traverse the string is by using the Python range function. This method lets us access string elements using the index. ... """ Python Program: Using range() to iterate over a string in Python """ string_to_iterate = "Data Science" for char_index in range(len(string_to_iterate)): print(string_to_iterate[char_index])
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
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
iterating over a list of strings
I want to append strings to temp, if they match particular pattern. Sounds like something a list comprehension could achieve. LIST = [EXPRESSION for ELEMENT in SEQUENCE if CONDITION] For example, the following loops over a list of strings and appends all strings containing an "a". my_list = [st for st in existing_list if "a" in st] You'd just need to figure our which regular expression to use as a condition. More on reddit.com
🌐 r/learnpython
10
1
April 18, 2022
Is there a way to "loop" through an integer?
while (number > 0) { sum += number % 10; number = number / 10; } More on reddit.com
🌐 r/csharp
76
44
April 6, 2018
🌐
Gitbooks
buzzcoder.gitbooks.io › codecraft-python › content › string › loop-through-a-string.html
Loop through a string · CodeCraft-Python - BuzzCoder
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[i].
🌐
W3Schools
w3schools.com › python › gloss_python_for_string.asp
Python For Looping Through a String
❮ Python Glossary · Even strings are iterable objects, they contain a sequence of characters: Loop through the letters in the word "banana": for x in "banana": print(x) Try it Yourself » · Python For Loops Tutorial For For Break For Continue Looping Through a Range For Else Nested Loops For pass ·
🌐
Python Guides
pythonguides.com › iterate-through-a-string-in-python
How To Iterate Through A String In Python?
March 19, 2025 - Learn how to iterate through a string in Python using loops like for and while, enumerate(), and list comprehensions. Includes examples and tips for beginners.
🌐
GeeksforGeeks
geeksforgeeks.org › python › iterate-over-characters-of-a-string-in-python
Iterate over characters of a string in Python - GeeksforGeeks
July 11, 2025 - The simplest way is to use a loop. Let’s explore this approach. The simplest way to iterate over the characters in a string is by using a for loop. This method is efficient and easy to understand.
Find elsewhere
🌐
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
🌐
Scientech Easy
scientecheasy.com › home › blog › iterate over string in python
Iterate over String in Python - Scientech Easy
January 26, 2026 - In this example, we have created a string “Python” and assigned it in a variable named language. We have initialized the index variable with a value zero. We have used a for loop that make it easy to loop or iterate over each character of a string.
🌐
Oraask
oraask.com › home › wiki - article
Python Iterate String - All you need to know - Oraask
October 15, 2022 - Using string[start position (-): stop position (-): incremental step (-)], we can iterate through a portion of our string variable backward by creating a small slice of our string by using the [] string slicing operator. mystring = "Hi Python" ...
🌐
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
🌐
Java2Blog
java2blog.com › home › python › python string › loop through string in python
Loop through String in Python - Java2Blog
October 28, 2021 - We can loop through a string and display every character individually. To achieve this, we can use the for loop and the while loop available in Python. The for loop is the most basic method to iterate over any sequence.
🌐
LaunchCode
education.launchcode.org › lchs › chapters › strings › string-iteration.html
7.8. Iteration with Strings — LaunchCode's LCHS documentation
Loop through the string using negative index values. Run the program with range(-1, -len(range), -1). How does the output change? range(-1, -len(range), -1) does not quite get all of the letters from name. Modify the start, stop, step values as needed to print all of the characters. Since a string is simply a sequence of characters, Python gives us a way to automatically iterate over those characters.
🌐
Playbackpress
playbackpress.com › books › pybook › chapter › 2 › 1
Iterating Through a String
There are many libraries and frameworks that use Python for things like data science, scientific computation, and web development. It is a great first language to learn. What you will see in this 'book' is a collection of code playbacks. Playbacks were designed to help guide a reader through code ...
🌐
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 - One of the most straightforward methods to loop over a string in Python is by using a for loop. This method is not only intuitive but also highly efficient for most use cases. When you use a for loop, you can directly access each character in ...
🌐
YouTube
youtube.com › watch
How to Iterate over a String in Python - YouTube
In this beginner-friendly tutorial, we’ll walk through multiple ways to loop through each character in a string using Python. Here is what you will learn1. F...
Published: May 24, 2025
🌐
Reddit
reddit.com › r/learnpython › iterating over a list of strings
r/learnpython on Reddit: iterating over a list of strings
April 18, 2022 -

I'm trying to loop over a list of strings. So how can I continue with iterating the inner loop with the outer Index.

l1 = [str1, str2, str3,str4,str5,str6,str7,str8,str9]

temp = []

for str in l1:

If re.search(r'.*4',str) :

for str in l1: if str is str8: break else: temp.append(str)

Output : temp = [ str5,str6,str7]

The above code is the pseudo code. I want to append strings to temp, if they match particular pattern.

Top answer
1 of 3
3
I want to append strings to temp, if they match particular pattern. Sounds like something a list comprehension could achieve. LIST = [EXPRESSION for ELEMENT in SEQUENCE if CONDITION] For example, the following loops over a list of strings and appends all strings containing an "a". my_list = [st for st in existing_list if "a" in st] You'd just need to figure our which regular expression to use as a condition.
2 of 3
2
It looks like the behavior you want is to go through the loop, but only start doing things once a certain condition becomes true. You could use a flag for that, and only use a single loop: condition_reached = False for string in list_of_strings: if condition(string): condition_reached = True: if condition_reached: temp.append(string) That condition_reached is the flag. Once it's raised (set to True) it remains raised for each iteration after that, and you only do your work if that flag is raised. There's actually a standard library module that can accomplish the same thing: itertools.dropwhile. You pass it an iterable (the list of strings) and a key (a function that returns a boolean) and it returns a new iterable that drops the front elements of the original while the key is true: def string_is_not_number_4(string): return string != "str4" for string in dropwhile(string_is_not_number_4, list_of_strings): temp.append(string) then temp would be ["str4", "str5", ... , "str9"], having cut out the front up until string_is_not_number_4 returned true.
🌐
Replit
replit.com › discover › how-to-iterate-through-a-string-in-python
Discover | Replit
Describe what you want. Replit builds it. Get a working app or website in minutes. No coding required.
🌐
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.