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
🌐
Runestone Academy
runestone.academy › ns › books › published › fopp › Iteration › Stringsandforloops.html
7.4. Strings and for loops — Foundations of Python Programming
Note that the for loop processes the characters in a string or items in a sequence one at a time from left to right. ... Iteration by item will process once for each item in the sequence. ... The blank is part of the sequence. ... 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")
🌐
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
🌐
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].
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-string.html
Python Strings
Each character in a Python string is a unicode character, so characters for all languages are supported.
Find elsewhere
🌐
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.
🌐
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_strings.asp
Python Strings
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.
🌐
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.
🌐
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...
🌐
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.

🌐
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),...
🌐
Reddit
reddit.com › r/learnprogramming › using while loops to find a character in a string
r/learnprogramming on Reddit: Using while loops to find a character in a string
December 20, 2022 -

I want to use the while loop to return the position of a character in a string.

I found this solution:

x = "abcdefg"
while "e" in x:
      print("the character e has been found at position", x.index("e"))
      break

I did the same thing with a for loop. Ignore the bad sintax please.

count =0 
for i in "abcdefg...."
  if i=="j":
    print(count)
break
  else:
       count=count+1
print(count)

Ok, so... I would like to have an alternative solution with while, that has a similar logic to this last one.

🌐
Devcamp
bottega.devcamp.com › full-stack-development-javascript-python › guide › how-to-loop-through-characters-python-string
How to Loop Through the Characters of a Python String
This is going to be a quick guide ... in python and we see that the for in loop can also be used with strings. ... So if I have a string that contains a portion of the alphabet. I could say alphabet and inside of that put abcdef. ... and if I want to loop through each one of those items a 4 in the loop allows me to access them just like they were in a collection because if you think about it a string is really just a collection of characters so it's almost ...
🌐
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='')
🌐
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 += ch return result · See the guide: Python List for more details about lists