Where is this text coming from? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Where is this text coming from?

When I run the code with open('file.txt') as file: line_number = 1 for line in file: print('{}: {}'.format(line_number, line.rstrip())) line_number += 1 Python outputs 1: This is line one. 2: This is line two. 3: Finally, we are on the third and last line of the file. How does it know what to say?! I never told it to print a string saying "This is line one". Also, why does it stop at three? Any help would be appreciated, thanks!

2nd Jul 2021, 10:17 PM
Caleb Eldredge
Caleb Eldredge - avatar
1 Antwort
+ 3
Your code loads a text file and loops through the lines and prints them.
2nd Jul 2021, 10:24 PM
Simon Sauter
Simon Sauter - avatar