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
How do you make a program iterate over a string (which is already a variable) a set amount of times?
Python - Create string with for-loop - Stack Overflow
Confused about for loops in Python. Please help me break down this code.
Is there a way for a for loop to go through a string and also include spaces.
Videos
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.
Your loop is currently just looping through the characters of summer_word. The name "consonants" you give in "for consonants..." is just a dummy variable, it doesn't actually reference consonants that you defined. Try something like this:
consonants = "qwrtpsdfghjklzxcvbnm" # This is fine don't need a list of a string.
summer_word = "icecream"
new_word = ""
for character in summer_word: # loop through each character in summer_word
if character in consonants: # check whether the character is in the consonants list
new_word += character
else:
continue # Not really necessary by adds structure. Just says do nothing if it isn't a consonant.
ANSWER = new_word
A string in Python is already a list of characters and may be treated as such:
In [3]: consonants = "qwrtpsdfghjklzxcvbnm"
In [4]: summer_word = "icecream"
In [5]: new_word = ""
In [6]: for i in summer_word:
...: if i in consonants:
...: new_word += i
...:
In [7]: new_word
Out[7]: 'ccrm'
I am an absolute beginner in programming, so please forgive my utter lack of understanding.
I came across this concept quite recently through a course I am taking in Python. I re-watched the lessons, I have tried doing some separate research on this topic, but I am still having a hard time wrapping my head around it.
The project I am currently working on is a random password generator. I was stumped and couldn't figure it out on my own so I watched the teacher's solution as a last resort. Especially puzzled when she used this code:
password = ""
for char in password_list:
password += charFor context, this code is supposed to convert a list of characters (from password_list) into a string.
I thought that for loops were used for iteration. I am only familiar with using "for __ in __ range(a, b)" so far, so I was quite puzzled by this loop as I don't understand how it works. Could someone please help me break it down into simpler terms? That would be super appreciated.
Please and thank you.
For example,
I have the string “Hello? Hi\n”
The \n just represents a new line. When I make a for loop going through this string I append how many times a certain character occurs. it only includes the letters and the question mark. How can I make it also include the amount of spaces or \n I have too?
That's not how you do it.
>>> ''.join(['first', 'second', 'other'])
'firstsecondother'
is what you want.
If you do it in a for loop, it's going to be inefficient as string "addition"/concatenation doesn't scale well (but of course it's possible):
>>> mylist = ['first', 'second', 'other']
>>> s = ""
>>> for item in mylist:
... s += item
...
>>> s
'firstsecondother'
endstring = ''
for s in list:
endstring += s