Search code examples
pythonvariables

Make a variable from what's in a text file


So I want to make a variable from the contents of a text file.

E.g The text file contains the sentence "Ask not what your country can do for you ask what you can do for your country"

Now I want the variable to be a string which contains the sentence in text file: Sentence = ("Ask not what your country can do for you ask what you can do for your country")

But import the string value from the text file, if that makes any sense?


Solution

  • The file contains.

    rahul@HP-EliteBook ~/Projects/Stackoverflow $ cat abc.txt 
    hai am here
    

    Here is the python code.It's very simple logic.

    fo = open("abc.txt", "r+")
    a = fo.read()