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
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
How do i type in {, }, " characters into a string?
in computer world, in many cases if you want to point to the exact character and not its "programming" meaning, you can "escape" it; that is writing "here is a doublequote for you: \" and here is a backslash \\". In addition, in fstrings you can user "{{" to print a {. More on reddit.com
🌐 r/learnpython
2
0
August 2, 2024
🌐
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
🌐
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.
🌐
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.
🌐
TechBeamers
techbeamers.com › iterate-strings-python
Python Program: 5 Ways to Iterate Strings - TechBeamers
November 30, 2025 - You can take the slice operator usage further by using it to iterate over a string but leaving every alternate character. Check out the below example: """ Python Program: Using slice [] operator to iterate over a specific parts of a string """ string_to_iterate = "Python_Data_Science" for char in string_to_iterate[ : : 2]: print(char)
🌐
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].
🌐
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...
🌐
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 ...
🌐
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),...
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-string.html
Python Strings
See the unicode section below for more information. The len() function returns the length of a string, the number of chars in it. It is valid to have a string of zero characters, written just as '', called the "empty string". The length of the empty string is 0. The len() function in Python is omnipresent - it's used to retrieve the length of every data type, with string just a first example...
🌐
Python
docs.python.org › 3 › library › string.html
Common string operations — Python 3.14.7 documentation
It is exposed as a separate function for cases where you want to pass in a predefined dictionary of arguments, rather than unpacking and repacking the dictionary as individual arguments using the *args and **kwargs syntax. vformat() does the work of breaking up the format string into character data and replacement fields.
🌐
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 ...
🌐
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)