Last updated: Apr 8, 2024
Reading timeยท5 min
To add quotes to a string in Python:
# โ Alternating single and double quotes result_1 = '"apple"' # ------------------------------------- # โ Add double quotes around a variable my_str = 'hello world' result = f'"{my_str}"' print(result) # ๐๏ธ "hello world" # ------------------------------------- # โ Add single quotes around a variable my_str = "hello" result = f"'{my_str}'" print(result) # ๐๏ธ 'hello' # ------------------------------------- # โ Escaping double quotes with a backslash result_3 = "\"apple\""
The first example in the code sample alternates between single and double quotes.
result = '"apple"'
If a string is wrapped in single quotes, we can use double quotes in the string without any issues.
If you need to add single quotes to a string, wrap the string in double quotes.
result = "one 'two' three"
In some rare cases, your string might contain both single and double quotes. To get around this, use a triple-quoted string.
result_1 = """ "one" two 'three' """
Triple-quotes strings are very similar to basic strings that we declare using single or double quotes.
But they also enable us to:
example = ''' It's Alice "hello" ''' # # It's Alice # "hello" # print(example)
The string in the example above uses both single and double quotes and doesn't have to escape anything.
You can use a formatted string literal to add double or single quotes around a variable in Python.
Formatted string literals let us include variables inside of a string by
prefixing the string with f
.
my_str = 'one' result = f'"{my_str}" "two"' print(result) # ๐๏ธ '"one" "two"'
Notice that we still have to alternate between single and double quotes.
Formatted string literals (f-strings) let us include expressions inside of a
string by prefixing the string with f
.
my_str = 'is subscribed:' my_bool = True result = f'{my_str} "{my_bool}"' print(result) # ๐๏ธ 'is subscribed: "True"'
Make sure to wrap expressions in curly braces - {expression}
.
You can also use a backslash \
to escape quotes.
result = "\"one\" \"two\"" print(result) # ๐๏ธ '"one" "two"'
In most cases, it is preferable (and more readable) to alternate between single and double quotes, but escaping quotes can also be useful (e.g. in rare cases in a JSON 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.
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.
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.
variable1 = 'bobbyhadz' variable2 = 'website' result = f"""'{variable1}' "{variable2}\"""" print(result) # ๐๏ธ 'bobbyhadz' "website"
Triple-quoted strings are very similar to basic strings that we declare using single or double quotes.
The string in the example uses both single and double quotes and doesn't have to escape anything.
To print a variable inside quotation marks:
str.format()
method to wrap the variable in quotes.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 {}
.
format()
method as you have replacement fields in the string.You can also include the quotes in the variable declaration.
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.
To join a list of strings wrapping each string in quotes:
join()
method on a string separator.join()
method.my_list = ['one', 'two', 'three'] my_str = ', '.join(f'"{item}"' for item in my_list) print(my_str) # ๐๏ธ '"one", "two", "three"'
The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
my_list = ['one', 'two', 'three'] print(', '.join(my_list)) # ๐๏ธ 'one, two, three'
The string the method is called on is used as the separator between elements.
my_list = ['one', 'two', 'three'] my_str = ' '.join(f'"{item}"' for item in my_list) print(my_str) # ๐๏ธ '"one" "two" "three"'
If you don't need a separator and just want to join the iterable's elements into
a string, call the join()
method on an empty string.
We used a formatted string literal to wrap each list item in quotes.
f
.Make sure to wrap expressions in curly braces - {expression}
.
The last step is to use a generator expression to iterate over the list of strings.
my_list = ['one', 'two', 'three'] my_str = ', '.join(f'"{item}"' for item in my_list) print(my_str) # ๐๏ธ '"one", "two", "three"'
In the example, we iterated over the list and wrapped each item with quotes.
This approach also works if the list contains values of different types (e.g. integers).
my_list = ['one', 'two', 'three', 4, 5] my_str = ', '.join(f'"{item}"' for item in my_list) print(my_str) # ๐๏ธ '"one", "two", "three", "4", "5"'
The join()
method raises a TypeError
if there are any non-string values in
the iterable, but we take care of converting each list item to a string with the
f-string.
You can learn more about the related topics by checking out the following tutorials: