How to add Quotes to a String in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
5 min

banner

# Table of Contents

  1. Add quotes to a string in Python
  2. Add double or single quotes around a variable in Python
  3. Print a variable inside quotation marks using str.format()
  4. Join a list of strings wrapping each string in quotes in Python

# Add quotes to a string in Python

To add quotes to a string in Python:

  1. Alternate between single and double quotes.
  2. For example, to add double quotes to a string, wrap the string in single quotes.
  3. To add single quotes to a string, wrap the string in double quotes.
main.py
# โœ… 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\""

add quotes to string in python

The code for this article is available on GitHub

The first example in the code sample alternates between single and double quotes.

main.py
result = '"apple"'

# Alternate between single and double quotes

If a string is wrapped in single quotes, we can use double quotes in the string without any issues.

However, if we try to use single quotes in a string that was wrapped in single quotes, we end up terminating the string prematurely.

If you need to add single quotes to a string, wrap the string in double quotes.

main.py
result = "one 'two' three"

# Using a triple-quoted string

In some rare cases, your string might contain both single and double quotes. To get around this, use a triple-quoted string.

main.py
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:

  • use single and double quotes in the same string without escaping
  • define a multiline string without adding newline characters
main.py
example = ''' It's Alice "hello" ''' # # It's Alice # "hello" # print(example)
The code for this article is available on GitHub

The string in the example above uses both single and double quotes and doesn't have to escape anything.

End of lines are automatically included in triple-quoted strings, so we don't have to add a newline character at the end.

# Add double or single quotes around a variable in Python

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.

main.py
my_str = 'one' result = f'"{my_str}" "two"' print(result) # ๐Ÿ‘‰๏ธ '"one" "two"'

add double or single quotes around variable

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.

main.py
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.

main.py
result = "\"one\" \"two\"" print(result) # ๐Ÿ‘‰๏ธ '"one" "two"'
The code for this article is available on GitHub

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).

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.

main.py
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.

main.py
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.

main.py
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.

# Print a variable inside quotation marks using str.format()

To print a variable inside quotation marks:

  1. Use the str.format() method to wrap the variable in quotes.
  2. Use the print() function to print the result.
main.py
variable = 'bobbyhadz.com' result = '"{}"'.format(variable) print(result) # ๐Ÿ‘‰๏ธ "bobbyhadz.com"

print variable inside quotation marks using str format

The code for this article is available on GitHub

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.

main.py
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.

# Join a list of strings wrapping each string in quotes in Python

To join a list of strings wrapping each string in quotes:

  1. Call the join() method on a string separator.
  2. Pass a generator expression to the join() method.
  3. On each iteration, use a formatted string literal to wrap the item in quotes.
main.py
my_list = ['one', 'two', 'three'] my_str = ', '.join(f'"{item}"' for item in my_list) print(my_str) # ๐Ÿ‘‰๏ธ '"one", "two", "three"'

join list of strings wrapping each string in quotes

The code for this article is available on GitHub

The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

main.py
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.

main.py
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.

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with 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.

main.py
my_list = ['one', 'two', 'three'] my_str = ', '.join(f'"{item}"' for item in my_list) print(my_str) # ๐Ÿ‘‰๏ธ '"one", "two", "three"'
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

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).

main.py
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 code for this article is available on GitHub

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev