Print a variable inside quotation marks in Python

avatar

Borislav Hadzhiev

Last updated: Sep 8, 2022

banner

Photo from Unsplash

Print a variable inside quotation marks in Python #

To print a variable inside quotation marks:

  1. Use a formatted string literal to add quotes around the variable.
  2. F-strings let us include variables inside of a string by prefixing the string with f.
  3. Use the print() function to print the result.
main.py
variable = 'bobbyhadz.com' result = f'"{variable}"' print(result) # 👉️ "bobbyhadz.com" result = f'website: "{variable}"' print(result) # 👉️ website: "bobbyhadz.com"
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
var1 = 'bobby' var2 = 'hadz' result = f'{var1}"{var2}"' print(result) # 👉️ bobby"hadz"

Make sure to wrap expressions in curly braces - {expression}.

Notice that we wrapped the f-string in single quotes to be able to use double quotes inside of the string.

It is important to alternate between single and double quotes because otherwise you'd terminate the f-string prematurely.

If you have to print the variable in single quotes, wrap the f-string in double quotes.

main.py
variable = 'bobbyhadz.com' result = f"'{variable}'" print(result) # 👉️ 'bobbyhadz.com'

If you have to include both single and double quotes in the string, use a triple-quoted string.

main.py
variable1 = 'bobbyhadz' variable2 = 'website' result = f"""'{variable1}' "{variable2}" """ print(result) # 👉️ 'bobbyhadz' "website"

If you need to have a double quote right next to the double quotes that terminate the triple-quoted string, escape it.

main.py
variable1 = 'bobbyhadz' variable2 = 'website' result = f"""'{variable1}' "{variable2}\"""" print(result) # 👉️ 'bobbyhadz' "website"

Triple-quotes strings are very similar to basic strings that we declare using single or double quotes.

But they also enable us to:

  • use single and double quotes in the same string without escaping
  • define a multi-line string without adding newline characters

Alternatively, you can use the str.format() method.

Print a variable inside quotation marks using str.format() #

To print a variable inside quotation marks:

  1. Use the str.format() method to wrap the variable in quotes.
  2. Use the print() function to print the result.
main.py
variable = 'bobbyhadz.com' result = '"{}"'.format(variable) print(result) # 👉️ "bobbyhadz.com"

The str.format method performs string formatting operations.

The string the method is called on can contain replacement fields specified using curly braces {}.

Make sure to provide exactly as many arguments to the format() method as you have replacement fields in the string.

You can also include the quotes in the variable declaration.

main.py
variable = 'website "bobbyhadz"' print(variable) # 👉️ website "bobbyhadz"

Note that we used single quotes to wrap the string and double quotes inside of it.

Had we used single quotes inside of the string without escaping them, we'd terminate the string prematurely.

If you need to add both single and double quotes in a string, use a triple-quoted string.

main.py
variable = """'bobbyhadz' "website" """ print(variable) # 👉️ 'bobbyhadz' "website"

If you need to have a double quote right next to the double quotes that terminate the triple-quoted string, escape it.

main.py
variable = """'bobbyhadz' "website\"""" print(variable) # 👉️ 'bobbyhadz' "website"
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.