Update :
From Python 3.6, you can use f-strings
>>> print[f'"{word}"']
"Some Random Word"
Original Answer :
You can try %-formatting
>>> print['"%s"' % word]
"Some Random Word"
OR
str.format
>>> print['"{}"'.format[word]]
"Some Random Word"
OR escape the quote character with \
>>> print["\"%s\"" % word]
"Some Random Word"
And, if the double-quotes is not a restriction [i.e. single-quotes would do]
>>> from pprint import pprint, pformat
>>> print[pformat[word]]
'Some Random Word'
>>> pprint[word]
'Some Random Word'
OR like others have already said [include it in your declaration]
>>> word = '"Some Random Word"'
>>> print[word]
"Some Random Word"
Use whichever you feel to be better or less confusing.
And, if you need to do it for multiple words, you might as well create a function
def double_quote[word]:
return '"%s"' % word
print[double_quote[word], double_quote[word2]]
And [if you know what you're doing &] if you're concerned about performance of these, see this comparison.
Print a variable inside quotation marks in Python #
To print a variable inside quotation marks:
- Use a formatted string literal to add quotes around the variable.
- F-strings let us include variables inside of a string by prefixing the string with
f
. - Use the
print[]
function to print the result.
Copied!
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
.
Copied!
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.
Copied!
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.
Copied!
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.
Copied!
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 multiline 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:
- Use the
str.format[]
method to wrap the variable in quotes. - Use the
print[]
function to print the result.
Copied!
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.
Copied!
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.
Copied!
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.
Copied!
variable = """'bobbyhadz' "website\"""" print[variable] # 👉️ 'bobbyhadz' "website"