Search code examples
pythontext-filesquotes

How to write variables in between single quotes in a textfile in python?


I have a bunch of strings to write in a text file. I want the strings to be written in simple quotes in the file, so I tried using this code: file.write("the variable is = \"" + my_string + "\" ") I wanted the output in the txt file to be: the variable is = 'my_string' but instead I get the variable is = "my_string" in double quotes.

What should I do to get the right output?


Solution

  • Thanks to you all for your advice !! It worked well with the solution proposed by Deepak Tripathi, which is file.write(f"the variable is = '{variable_name}' ") ! Thanks, thats exactly what i nedeed :)