How to Read String from File in Python

To read a string from a file in Python, open a file using with open() function and then use the file.read() function to read a string from the file. The read() is a built-in Python function that reads the file’s content.

We can read strings from the file by using the read() function. We can store the read content in the string variable. Python string is a built-in data type. The string consists of a sequence of characters.

Create a text file called sample.txt and insert the following string inside the text file.

I Love Python Programming

To read the above string from a file, use the with open() function to open a file in read mode and then use the file.read() function to read a string and print in the console.

with open("sample.txt", "r") as file:
 content = file.read()
print(content)

Output

I Love Python Programming

In this program, we are reading a string from a file using a read() function. In this program, we opened a file named sample.txt in the read mode. In the next line, we read the contents of the file using the read() function. 

The read() function reads all the content of the file. Even the tab and new lines are also read from the file. In the last line, we have printed the content of the file.

How to read the string from a file without using newlines

To read a string from a file without using newlines in Python, use the file.read().replace(‘\n’, ‘ ‘) function.

with open("sample.txt", "r") as file:
 content = file.read().replace('\n', ' ')
print(content)

Output

I Love Python Programming

In this program, we replaced all the new line characters with a space. This program is similar to the previous one. The only difference is that, if new lines character is present in the sample.txt then the previous program reads the new line also and keeps it in the string.

But in this program, the new line character will be replaced by a space character. The content is then printed without newlines.

Using readlines() function

The readlines() is a built-in Python function that returns a list containing each line in the file as a list element. The readlines() function can be used for small files, as it reads the whole file content to the memory, then split it into separate lines.

with open("sample.txt", "r") as file:
 content = file.readlines()
 data = " ".join(content)
 print( data )

Output

I Love Python Programming

In this program, we opened a file called the sample.txt in reading mode. In the next line, we read all the lines present in the file. Each line is added as a separate element in a list. 

The readlines() function reads each and every line in a file and stores it as a list. We then join the list elements by using spaces to make the list as a string. And then we print that string contents. 

Hence, we saw how to read a string from a file using the read() and readlines() functions.

That’s it for this tutorial.

See also

How to Import Files in Python

How to Convert File to String in Python

How to Save Array to File in Python

Leave a Comment