readline() in Python Last Updated : 21 Apr, 2025 Comments Improve Suggest changes Like Article Like Report The readline() method in Python is used to read a single line from a file. It is helpful when working with large files, as it reads data line by line instead of loading the entire file into memory.Syntaxfile.readline(size)Parameterssize (Optional): The number of bytes from the line to return. Default -1, which means the whole line.Return ValueReturns an empty string ('') when the end of the file is reached.Examples of readline() First, let's create a file called example.txt with the following content:This is the first line.This is the second line.This is the third line.This is the fourth line.1. Reading a Single Line Python with open("example.txt", "r") as file: line = file.readline() print(line) # Prints the first line of the file Output:This is the first line.Explanation:file.readline() reads the first line from the file.It prints the first line, which is "This is the first line.", including the newline character (\n) at the end of the line.2. Reading Multiple Lines with a Loop Python with open("example.txt", "r") as file: while True: line = file.readline() if not line: break # Stop when end of file is reached print(line.strip()) Output:This is the first line.This is the second line.This is the third line.This is the fourth line.Explanation:The while True: loop keeps reading lines until the end of the file is reached. file.readline() reads one line at a time.if not line: checks if the line is empty (which happens when the end of the file is reached). When it finds an empty line, it breaks out of the loop.line.strip() removes any trailing newline characters from the line before printing it.3. Using readline() with a Specific Character Limit Python with open("example.txt", "r") as file: line = file.readline(10) print(line) Output:This is theExplanation:file.readline(10) reads the first 10 characters of the first line in the file. It stops reading after the 10th character, regardless of whether it's at the end of the word or not.The print(line) statement will print exactly those 10 characters.Difference Between readline(), readlines(), and read()MethodDescriptionreadline()Reads one line at a timereadlines()Reads all lines and returns them as a listread()Reads the entire file as a single string Comment More infoAdvertise with us Next Article readline() in Python P pragatikheuvg Follow Improve Article Tags : Python Python Programs Practice Tags : python Similar Reads Python String Input Output In Python, input and output operations are fundamental for interacting with users and displaying results. The input() function is used to gather input from the user and the print() function is used to display output.Input operations in PythonPythonâs input() function allows us to get data from the u 3 min read Python Unpack List Unpacking lists in Python is a feature that allows us to extract values from a list into variables or other data structures. This technique is useful for various situations, including assignments, function arguments and iteration. In this article, weâll explore what is list unpacking and how to use 3 min read Python Access Tuple Item In Python, a tuple is a collection of ordered, immutable elements. Accessing the items in a tuple is easy and in this article, we'll explore how to access tuple elements in python using different methods.Access Tuple Items by IndexJust like lists, tuples are indexed in Python. The indexing starts fr 2 min read Literals in Python Literals in Python are fixed values written directly in the code that represent constant data. They provide a way to store numbers, text, or other essential information that does not change during program execution. Python supports different types of literals, such as numeric literals, string litera 4 min read How to Initialize a String in Python In Python, initializing a string variable is straightforward and can be done in several ways. Strings in Python are immutable sequences of characters enclosed in either single quotes, double quotes or triple quotes. Letâs explore how to efficiently initialize string variables.Using Single or Double 2 min read Like