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 OverflowAs 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
If you need access to the index as you iterate through the string, use enumerate():
for index, character in enumerate('test'):
print(index, character)
...
0 t
1 e
2 s
3 t
python - result = [ch for ch in string if (ch != ' ')]; result = str(result); print(result); prints list, not string - Stack Overflow
How do you make a program iterate over a string (which is already a variable) a set amount of times?
Not really understanding when to use for I in string vs for I in range(len(string))
Using while loops to find a character in a string
str() on a list does not magically turn said list into a no delimiter string. When you do
str(['a', 'b', 'c']) - what you actually get is '['a', 'b', 'c']', which is indeed a string.
If you'd like the result to be 'abc' please, use .join.
''.join(['a', 'b', 'c'])
Output-
'abc'
Here result = [ch for ch in string if (ch != ' ')] you eliminate spaces and save the chars into list. But to have string again, you need to join them:
string = 'a b c'
result = [ch for ch in string if (ch != ' ')]
s = "".join(result)
print(s) # abc
Here is the related doc page.
I'm new to python and one of the exercises they gave us at school requires me to make the program iterate over a string a set amount of times.
The issue is that if I use the "for" loop (aka: for i in range()), there is nowhere to put which variable I want it to iterate over, and if I put "for i in (variable)", I can't tell it how many times it needs to do it and just ends up iterating the whole string. Any input on the matter is appreciated.
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.
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"))
breakI 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.