How to read a text file into a string variable and strip newlines? You could use: with open('data.txt', 'r') as file: data = file.read().replace('\n', '')