Skip to content

How to add Quotes to a String in Python

Learn how to enclose strings in Python with single or double quotes using methods like f-strings, escaping, or alternating quotation marks for proper string formatting.

Adding quotes to strings in Python is a common task for formatting output, preparing data, and creating specialized strings.

This guide will explore several methods for enclosing strings within single or double quotes, including techniques for escaping, and joining lists of strings with quotes.

Adding Quotes by Alternating Quotation Marks

The simplest method for adding quotes is to alternate between single and double quotes. To add double quotes to a string, wrap the string in single quotes, and vice versa.

result_1 = '"apple"' # Double quotes around the string "apple"

result_2 = "'orange'" # Single quotes around the string "orange"
print(result_1) # Output: "apple"
print(result_2) # Output: 'orange'

This is the most readable way to create a simple string with quotation marks.

Adding Quotes Around Variables with f-strings

Formatted string literals (f-strings) provide an efficient way to add quotes around variables.

my_str = 'hello world'
result_double = f'"{my_str}"' # Double quotes around the variable
print(result_double) # Output: "hello world"

my_str = "hello"
result_single = f"'{my_str}'" # Single quotes around the variable
print(result_single) # Output: 'hello'

By alternating between single and double quotes, you can easily enclose a variable within quotation marks.

Escaping Quotes with Backslashes

You can also use backslashes (\) to escape quotation marks within a string. This lets you use the same type of quote as the string delimiters.

result_1 = "\"apple\"" # Escaping double quotes
result_2 = '\'orange\'' # Escaping single quotes

print(result_1) # Output: "apple"
print(result_2) # Output: 'orange'

Using Triple-Quoted Strings

Triple-quoted strings, delimited by either ''' or """, allow you to include both single and double quotes without escaping, and allow you to define multiline strings without explicit newlines:

example = '''
    It's Alice
    "hello"
'''
print(example)

output:

    It's Alice
    "hello

Printing Variables Inside Quotes with str.format()

The str.format() method provides an alternative way to print variables inside quotes.

variable = 'tutorialreference.com'
result = '"{}"'.format(variable)
print(result) # Output: "tutorialreference.com"

The {} is replaced with the passed in argument to the format() method.

Joining a List of Strings with Quotes

To join a list of strings while wrapping each string in quotes, use a generator expression with str.join() and an f-string.

my_list = ['one', 'two', 'three']
my_str = ', '.join(f'"{item}"' for item in my_list)
print(my_str) # Output: "one", "two", "three"

my_str_spaces = ' '.join(f'"{item}"' for item in my_list)
print(my_str_spaces) # Output: "one" "two" "three"

my_str_no_separator = ''.join(f'"{item}"' for item in my_list)
print(my_str_no_separator) # Output: "one""two""three"

This example demonstrates how to use generator expression within str.join() in combination with f-strings.

Navigation