text files there is ton of interesting data to be found on the internet stored in text files in this we will learn how to work with data stored in text files reading from files suppose we have text file called example txt whose contents are shown belowand we want to read its contents into python there are several ways to do so we will look at two of them hello this is text file bye the first way to read text file uses list comprehension to load the file line-by-line into listlines [line strip(for line in open'example txt ')the list lines is now 'hello ''this is text file ''bye'the string method strip removes any whitespace characters from the beginning and end of string if we had not used iteach line would contain newline character at the end of the line this is usually not what we want notestrip removes whitespace from both the beginning and end of the line use rstrip if you need to preserve whitespace at the beginning of the line the second way of reading text file loads the entire file into strings open'example txt 'read(the string is now 'hello \nthis is text file \nbye