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
🌐
GeeksforGeeks
geeksforgeeks.org › python › iterate-over-characters-of-a-string-in-python
Iterate over characters of a string in Python - GeeksforGeeks
July 11, 2025 - In this article, we will learn ... in Python. There are several methods to do this, but we will focus on the most efficient one. 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 ...
Discussions

python - result = [ch for ch in string if (ch != ' ')]; result = str(result); print(result); prints list, not string - Stack Overflow
The for loop within this list takes each value from the string, one at a time, and places it into the list - this is why the list ['a', 'b', 'c'] appears in your code. To obtain a string without spaces from a string with spaces: Copystring = 'a b c' # obtain characters from the string that ... More on stackoverflow.com
🌐 stackoverflow.com
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
Not really understanding when to use for I in string vs for I in range(len(string))
For strings, there is basically zero reason to use the latter. If you need access to the index, use enumerate. For lists, you use the latter when you are changing the list that you're iterating over. More on reddit.com
🌐 r/learnpython
14
8
September 27, 2020
Using while loops to find a character in a string
s = "abcdef" currIndex = 0 length = len(s) while currIndex < length: if s[currIndex] == 'e': print(currIndex) currIndex += 1 create an index variable (currIndex) set to the starting point of the string, 0. create a limit variable for your index (length), in this case the limit is the length of the string. set your while loop to execute as long as currIndex is less than the limit. keep in mind the length of the string will always be one higher than the last index..hense "<" instead of "<=". the first character is at index 0, and the last is at len(s)-1. each iteration of the while loop checks if the character of s at currIndex is equal to the char you're looking for and prints currIndex if it is. increment currIndex by 1 More on reddit.com
🌐 r/learnprogramming
6
1
December 20, 2022
🌐
Python Examples
pythonexamples.org › python-iterate-over-characters-of-a-string
Iterate over Characters of a String
x = input('enter a string: ') for ch in x: print(ch) ... 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.
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string
Python String - GeeksforGeeks
Use triple quotes ('''...''' ) or ( """...""") for strings that span multiple lines. Newlines are preserved. ... Strings are indexed sequences. Positive indices start at 0 from the left, negative indices start at -1 from the right as represented in below image: ... Example 1: Access specific characters through positive indexing.
Published: June 11, 2026
🌐
Runestone Academy
runestone.academy › ns › books › published › fopp › Iteration › Stringsandforloops.html
7.4. Strings and for loops — Foundations of Python Programming
... Yes, there are 12 characters, including the blank. Error, the for statement needs to use the range function. The for statement can iterate over a sequence item by item. How many times is the word HELLO printed by the following statements? s = "python rocks" for ch in s[3:8]: print("HELLO")
Find elsewhere
🌐
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.
🌐
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 built-in function that reports the size, or length, of its input, which is usually a string or a list. s = 'Apple pie' print(len(s)) # 9 print(len('Banana float')) # 12 len('') # 0 (an empty string) print(len('donuts') + len('pan cake')) # 14 · 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[i].
🌐
W3Schools
w3schools.com › python › python_strings.asp
Python Strings
However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string. Get the character at position 1 (remember that the first character has the position 0): a = "Hello, World!" print(a[1]) Try it Yourself » · Since strings are arrays, we can loop through the characters in a string, with a for loop.
🌐
Dot Net Perls
dotnetperls.com › for-python
Python - for: Loop Over String Characters - Dot Net Perls
January 4, 2025 - s = "abc" # Loop over string. for c in s: print(c) # Loop over string indexes. for i in range( ... A for-loop acts upon a collection of elements, not a min and max. In for, we declare a new variable. And after the in-keyword, we specify the collection we want to loop over.
🌐
W3Schools
w3schools.com › python › gloss_python_for_string.asp
Python For Looping Through a String
Python HOME Python Intro Python Get Started Python Syntax ... Python Variables Variable Names Assign Multiple Values Output Variables Global Variables Variable Exercises Code Challenge Python Data Types ... Python Strings Slicing Strings Modify Strings Concatenate Strings Format Strings Escape Characters String Methods String Exercises Code Challenge Python Booleans
🌐
Quora
quora.com › How-do-you-iterate-over-the-characters-of-a-string-in-Python
How to iterate over the characters of a string in Python - Quora
Answer (1 of 5): Very simply : [code]>>> my_string = 'This is my string' >>> for character in my_string: ... print(character) T h i s i s m y s t r i n g [/code]if you need to record the character and the index, you can use enumerate : [code]>>> my_string = 'This is my string' >>> for...
🌐
Quora
quora.com › How-can-we-use-for-loop-to-iterate-through-each-character-of-a-string
How can we use for loop to iterate through each character of a string? - Quora
Answer (1 of 2): The details vary, depending on the language. In Python, I would simply: [code]for character in my_string: print(character) [/code]Otherwise, if your programming language doesn’t allow this sort of construct, you can make a loop that changes a variable, from 1 to len(string),...
🌐
W3Schools
w3schools.in › python › strings
Python Strings - W3Schools
The program is showing the use of strings and how they are displayed on-screen. ... ch = 'Hello Python' str1 = "String Chapter" print ("First value is: " , ch) print ("Second value is: " , str1) ... If they are considered as a list of characters, then the example shown below will let you understand ...
🌐
PyTutorial
pytutorial.com › loop-through-string-characters-in-python-a-guide
PyTutorial | Loop Through String Characters in Python: A Guide
February 21, 2026 - The most common way is using a for loop. Python treats a string as an iterable sequence. The loop variable takes the value of each character, one by one. # Example 1: Basic character iteration my_string = "Hello" for char in my_string: print(char)
🌐
Stanford
web.stanford.edu › class › archive › cs › cs106a › cs106a.1214 › templates › handouts_w2021 › lecture-12.html
examples of features below: "foreach" and "in"
intersect(a, b): Given two strings, a and b. Return a version of a, including only those chars which also appear in b. Use a case-sensitive comparison. Use a for/ch/s loop. ... def intersect(a, b): result = '' # Look at all chars in a, check # each using "in" for ch in a: if ch in b: result ...
🌐
Reddit
reddit.com › r/learnpython › not really understanding when to use for i in string vs for i in range(len(string))
r/learnpython on Reddit: Not really understanding when to use for I in string vs for I in range(len(string))
September 27, 2020 -

Just looking for clarification for this very simple thing. Please correct me if I’m wrong but from what I understand, in for i in string, it takes each element in the string over the entire length. For i in range(len(string)) it indexes the elements and looks at the elements at each index so at position 0, string =‘x’ and at 1 string= ‘d’ ect.

If this is correct, I’m afraid I still don’t see the difference by way of when to use each or what purpose they serve.

🌐
TutorialsPoint
tutorialspoint.com › iterate-over-characters-of-a-string-in-python
Iterate over characters of a string in Python
July 1, 2020 - text = "tutorialspoint" # Iterate over the string for char in text: print(char, end='') ... text = "tutorialspoint" # Iterate with index and character for index, char in enumerate(text): print(f"{index}: {char}") 0: t 1: u 2: t 3: o 4: r 5: ...