Answers for "how to print line by line in python"

1

how to read a text file line by line in python

# Open the file with read only permit
f = open('my_text_file.txt')
# use readline() to read the first line 
line = f.readline()
# use the read line to read further.
# If the file is not empty keep reading one line
# at a time, till the file is empty
while line:
    # in python 2+
    # print line
    # in python 3 print is a builtin function, so
    print(line)
    # use realine() to read next line
    line = f.readline()
f.close()
Posted by: Guest on September-07-2020
1

how to print in one line python

# Python program to print list
# using for loop
a = [1, 2, 3, 4, 5]
  
# printing the list using loop
for x in range(len(a)):
    print(a[x], end = " ")
Posted by: Guest on June-21-2021

Code answers related to "how to print line by line in python"

Python Answers by Framework

Browse Popular Code Answers by Language