How to add double quotes around a variable in Python? That’s the topic I want to introduce to you today. There are 4 ways to do that, including using format string, str.format() function, json.dumps() function and putting quotes in the declaration. Read the following article.
Add double quotes around a variable in Python
Use f-string
Syntax:
f'a{value:pattern}b'
Parameters:
- f character: use the string f to format the string.
- a,b: characters to format.
- {value:pattern}: string elements need to be formatted.
- pattern: string format.
Example:
- String initialization.
- Use f-string to add quotes around.
- Double quotes surround the variable ‘myString’. The result returned quotes will surround the value of the variable.
myString = 'visit learnshareit website' # Use f-string to add double quotes addDoubleQuotes = f'"{myString}"' print('String after double quotes are added:', addDoubleQuotes)
Output:
String after double quotes are added: "visit learnshareit website"
Use % formatting
This type of formatting is familiar once you’ve been exposed to the C language.
Syntax:
<string>% (1st value, 2nd value, ….)
Example:
- String initialization.
- Use % formatting to add quotes around.
- That is, each part of the symbol %s will be replaced in turn by the values enclosed in parentheses.
myString = 'visit learnshareit website' # Use % formatting addDoubleQuotes = ('"%s"' % (myString)) print('String after double quotes are added:', addDoubleQuotes)
Output:
String after double quotes are added: "visit learnshareit website"
That is, each part of the symbol %s will be replaced in turn by the values enclosed in parentheses.
Use the str.format() function
Syntax:
str.format(value1, value2, ….)
Parameters:
- value: value to be formatted.
The format() method returns the formatted result of a given value specified by the specified formatting.
Example:
- Initialize a string.
- Use the str.format() to add double quotes.
myString = 'visit learnshareit website' # Use the str.format() function. addDoubleQuotes = ('"{}"'.format(myString)) print('String after double quotes are added:', addDoubleQuotes)
Output:
String after double quotes are added: "visit learnshareit website"
Alternate fields {} receive values from the format() function argument, then format them in the specified format and return the result to a string.
Use the json.dumps function
Syntax:
json.dumps()
The json.dumps function converts a subset of Python objects to a JSON string.
Example:
- Import the JSON module.
- Initialize a string.
- Use the json.dumps function to add double quotes.
import json myString = 'visit learnshareit website' # Use the json.dumps function to add double quotes addDoubleQuotes = json.dumps(myString) print('String after double quotes are added:', addDoubleQuotes)
Output:
String after double quotes are added: "visit learnshareit website"
Include quotes in the declaration
This is probably the simplest way. If you don’t remember the above formulas, you can use them.
Example:
myString = ' "visit learnshareit website" ' # Include quotes in the declaration print('String after double quotes are added:', myString)
Output:
String after double quotes are added: "visit learnshareit website"
Summary
So my article on how to add double quotes around a variable in Python is over. What do you think about this article? Which method can help you? If you have no idea, you should use method number three. Leave a comment to let us know what you think.
Maybe you are interested:
- Add spaces between the characters of a string in Python
- How to add space between variables in Python

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java