I am trying to apply for loop in the print statement in Python 3.7.
For example
string1="Liverpool is always alone"
string2="Manchester United is the best team in the world"
string3="Tottenham Hotspur is for losers"
string4="Leicester City is overrated"
for i in range(1,5):
print(string%i.find(" is")) # <---this is the problem
My ultimate aim is to get
9
17
17
14
Sure, I can store the results in a list and then print the results like this:
results=[string1.find(" is"),
string2.find(" is"),
string3.find(" is"),
string4.find(" is")]
for i in range(1,4):
print(results[i])
But it will be troublesome especially when the number of string becomes too much.
Please suggest a way to print the multiple strings using for loop.
I'm using Python 3.7.
string1, string2, ...is more troublesome than storing all the strings in a list calledstrings(without declaring the variables). If you store all of them in a list, you can simply havefor string in strings: print string.find(' is')range(1,4)will iterate on 1,2,3 only