Read File As String in Python
Last Updated :
28 Apr, 2025
Python provides several ways to read the contents of a file as a string, allowing developers to handle text data with ease. In this article, we will explore four different approaches to achieve this task. Each approach has its advantages and uses cases, so let's delve into them one by one.
Read File As String In Python
Below are some of the approaches by which we can read the file as a string in Python:
example.txt
This is the same file data that we will use throughout the article.
Hii,
I am a GeeksforGeeks student.
I am a web developer and DSA enthusiast
Read File As String Using read() Method
In this example, the file at the specified path ('example.txt') is opened, and its entire content is read into a string using the read() method. The with statement ensures proper handling of the file, automatically closing it after reading.
Python3
file_path = 'example.txt'
with open(file_path, 'r') as file:
file_content = file.read()
print(file_content)
Output:
Hii,
I am a GeeksforGeeks student.
I am a web developer and DSA enthusiast
Python Read File As String Using readline() Method
In this example, the file ('example.txt') is opened, and its content is read line by line in a loop using the readline() method. Each line is appended to the file_content string, creating a single string containing the entire file content.
Python3
file_path = 'example.txt'
with open(file_path, 'r') as file:
file_content = ''
line = file.readline()
while line:
file_content += line
line = file.readline()
print(file_content)
Output:
Hii,
I am a GeeksforGeeks student.
I am a web developer and DSA enthusiast
Read File As String Using readlines() Method
In this example, the file ('example.txt') is opened, and its content is read into a list of strings using the readlines() method. The lines are then joined into a single string using the join() method, creating the complete file content.
Python3
file_path = 'example.txt'
with open(file_path, 'r') as file:
lines = file.readlines()
file_content = ''.join(lines)
print(file_content)
Output:
Hii,
I am a GeeksforGeeks student.
I am a web developer and DSA enthusiast
Python Read File As String using pathlib Module
In this example, the pathlib module is employed to read the content of the file ('example.txt') as a string. The read_text() method is used on a Path object, providing a concise and modern approach to file reading.
Python3
from pathlib import Path
file_path = Path('example.txt')
file_content = file_path.read_text()
print(file_content)
Output:
Hii,
I am a GeeksforGeeks student.
I am a web developer and DSA enthusiast
Similar Reads
Reading binary files in Python Reading binary files means reading data that is stored in a binary format, which is not human-readable. Unlike text files, which store data as readable characters, binary files store data as raw bytes. Binary files store data as a sequence of bytes. Each byte can represent a wide range of values, fr
5 min read
How To Read .Data Files In Python? Unlocking the secrets of reading .data files in Python involves navigating through diverse structures. In this article, we will unravel the mysteries of reading .data files in Python through four distinct approaches. Understanding the structure of .data files is essential, as their format may vary w
4 min read
Reading and Writing to text files in Python Python provides built-in functions for creating, writing, and reading files. Two types of files can be handled in Python, normal text files and binary files (written in binary language, 0s, and 1s). Text files: In this type of file, Each line of text is terminated with a special character called EOL
8 min read
f-strings in Python Python offers a powerful feature called f-strings (formatted string literals) to simplify string formatting and interpolation. f-strings is introduced in Python 3.6 it provides a concise and intuitive way to embed expressions and variables directly into strings. The idea behind f-strings is to make
5 min read
How to Read from a File in Python Reading from a file in Python means accessing and retrieving the contents of a file, whether it be text, binary data or a specific data format like CSV or JSON. Python provides built-in functions and methods for reading a file in python efficiently.Example File: geeks.txtHello World Hello GeeksforGe
5 min read
Multiline String in Python A sequence of characters is called a string. In Python, a string is a derived immutable data typeâonce defined, it cannot be altered. To change the strings, we can utilize Python functions like split, join, and replace.Python has multiple methods for defining strings. Single quotations (''), double
4 min read
Read a file without newlines in Python When working with files in Python, it's common to encounter scenarios where you need to read the file content without including newline characters. Newlines can sometimes interfere with the processing or formatting of the data. In this article, we'll explore different approaches to reading a file wi
2 min read
How to read or input a string? In this article, we are going to learn how to print or output a string using different languages. Strings are considered a data type in general and are typically represented as arrays of bytes (or words) that store a sequence of characters. Strings are defined as an array of characters. Topics: How
3 min read
Python Raw Strings In Python, a raw string is a special type of string that allows you to include backslashes (\) without interpreting them as escape sequences. In this article, we will see how to take care of a backslash combined with certain alphabet forms literal characters which can change the entire meaning of th
6 min read
Read a file line by line in Python Python provides built-in functions for creating, writing, and reading files. Two types of files can be handled in Python, normal text files and binary files (written in binary language, 0s, and 1s). In this article, we are going to study reading line by line from a file.Example:Pythonwith open('file
4 min read