One topic about adding parentheses is how to add single quotes around a variable in Python. To do this, you can add parentheses around the variable or use Python’s built-in functions. Details of the methods are below.
Add single quotes around a variable in Python
Include quotes in the declaration
You can create a string with the single-quote character enclosed in the double-quote character. This can also be considered as adding single quotes around a variable.
Example:
testStr = " 'Learnshareit' " # Include single quotes in the declaration print('String after single quotes are added:', testStr)
Output:
String after single quotes are added: 'Learnshareit'
Note: do not use two parentheses at the same time. It will cause an error.
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 a string.
- Use the str.format() to add single quotes.
- Alternate fields {} receive values from the format() function argument, then format them in the specified format and return the result to a string.
- Note: Single quotes are placed inside double quotes.
testStr = 'Learnshareit' # Use the str.format() function to add single quotes addSingleQuotes = ("'{}'".format(testStr)) print('Add single quotes to the string:', addSingleQuotes)
Output:
Add single quotes to the string: '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:
- Create a string.
- Use f-string to add single quotes around.
- Note: Single quotes are placed inside double quotes. You also cannot use two parentheses at the same time.
testStr = 'Learnshareit' # Use f-string to add single quotes addSingleQuotes = f"'{testStr}'" print('Add single quotes to the string:', addSingleQuotes)
Output:
Add single quotes to the string: 'Learnshareit'
Use % formatting
This type of formatting is familiar once you’ve been exposed to the C language.
Syntax:
<string>% (1st value, 2nd value, ….)
Example:
- Create a string.
- Use % formatting to add single quotes around.
testStr = 'learnshareit' # Use % formatting addSingleQuotes = ("'%s'" % (testStr)) print('Add single quotes to the string:', addSingleQuotes)
Output:
Add single quotes to the string: 'learnshareit'
Summary
Those are the ways we use to add single quotes around a variable in Python. You can use any methods that work for you. But one thing we would like to repeat is not to use the same brackets simultaneously because it causes errors. Please comment below if you have any questions, and we will answer you.
Maybe you are interested:
- Test multiple variables against a single value in Python
- Check if multiple variables are equal in Python
- Check if a variable is a datetime object 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