This article will help you learn about how to add quotes to a string in Python, like using Python built-in functions or manually adding parentheses. Here are the details of the article. Please read it in its entirety!
Let’s learn how to add quotes to a string in Python
Use % formatting
Syntax:
<string>% (1st value, 2nd value, ….)
Example:
- Create string.
- Use % formatting to add quotes to the string.
- That is, each part of the symbol %s will be replaced in turn by the values enclosed in parentheses.
myString = 'visit learnshareit' # Use % formatting to add quotes to string resultQuotesStr = ('"%s"' % (myString)) print('String after adding quotes:', resultQuotesStr)
Output:
String after adding quotes: "visit learnshareit"
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:
- Create string.
- Use the str.format() to add quotes.
- Alternate fields {} receive values from the format() function argument, then format them in the specified format and return the result to a string.
myString = 'visit learnshareit' # Use the str.format() function to add quotes to string resultQuotesStr = ('"{}"'.format(myString)) print('String after adding quotes:', resultQuotesStr)
Output:
String after adding quotes: "visit learnshareit"
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' # Use f-string to add double quotes resultQuotesStr = f'"{myString}"' print('String after adding quotes:', resultQuotesStr)
Output:
String after adding quotes: "visit learnshareit"
Use the join function in combination with % formatting
Example:
myString = 'visit learnshareit' # Use the join function in combination with % formatting. resultQuotesStr = '"%s"' % " ".join([myString.strip() for i in myString.split("\n")]) print('String after adding quotes:', resultQuotesStr)
Output:
String after adding quotes: "visit learnshareit"
In the above example, the join function performs string concatenation after performing list comprehension. Finally, % formatting will add parentheses to that string.
Summary
So the article on how to add quotes to a string in Python is over. What is the best way for you? I like simplicity, so I use % formatting
. If you found the article helpful, please leave a comment. Thank you for reading!
Maybe you are interested:
- Add double quotes around a variable in Python
- How to compare a string with an Enum 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