How do you add a single quote to a string 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.

Copied!

# 👇️ alternating single and double quotes result_1 = '"apple"' # 👇️ using a formatted string literal my_str = 'apple' result_2 = f'"{my_str}"' # 👇️ escaping double quotes with a backslash result_3 = "\"apple\""

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

Copied!

result_1 = '"apple"'

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.

Copied!

result_1 = "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.

Copied!

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 multi-line string without adding newline characters

Copied!

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.

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

An alternative is to use a formatted string literal.

Copied!

my_str = 'one' result_2 = f'"{my_str}" "two"' print[result_2] # 👉️ '"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.

Copied!

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.

Copied!

result_3 = "\"one\" \"two\"" print[result_3] # 👉️ '"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].

To quote a string in Python use single quotation marks inside of double quotation marks or vice versa.

For instance:

example1 = "He said 'See ya' and closed the door."
example2 = 'They said "We will miss you" as he left.'

print[example1]
print[example2]

Output:

He said 'See ya' and closed the door.
They said "We will miss you" as he left.

Python Strings

Python strings are sequences of characters and numbers.

A string is wrapped around a set of single quotes or double quotes. There is no difference in which you use.

Anything that goes inside the quotes is interpreted as being “text” instead an executable command.

To demonstrate, here are some examples.

print["10 + 20"]                  # Prints: 10 + 20
print["This # is not a comment"]  # Prints: This # is not a comment
print["pow[2,3]"]                 # Prints: pow[2, 3]

In each example, there is a Python operation that would normally execute. But because the expression is wrapped inside a string, the expression is printed out as-is.

But here is where it gets interesting. Let’s see what happens when you place a double quote inside a string:

print["This "test" causes problems"]

Result:

  File "example.py", line 1
    print["This "test" causes problems"]
                 ^
SyntaxError: invalid syntax

This happens because the Python interpreter sees a string of the expression in three parts:

  1. "This "
  2. test
  3. " causes problems"

It sees two strings and a reference to a non-existent object test. Thus it has no idea what to do.

To come over this issue, you have two options:

  1. Use single quotes inside double quotes [and vice versa].
  2. Escape the quotes inside a string with a backslash.

1. Single Quotes inside Double Quotes

To write a quoted string inside another string in Python

  • Use double quotes in the outer string, and single quotes in the inner string
  • Use single quotes in the outer string and double quotes in the inner string

Here is an example:

example1 = "He said 'See ya' and closed the door."
example2 = 'They said "We will miss you" as he left.'

print[example1]
print[example2]

Output:

He said 'See ya' and closed the door.
They said "We will miss you" as he left.

But what if this is not enough? What if you want to have quotes inside quotes?

Then you need to resort to what is called escape sequences. These make it possible to add as many quotes in a string as you want.

2. How to Escape Quotes in a String

To add quoted strings inside of strings, you need to escape the quotation marks. This happens by placing a backslash [\] before the escaped character.

In this case, place it in front of any quotation mark you want to escape.

Here is an example.

example1 = "This is a \"double quote\" inside of a double quote"
example2 = 'This is a \'single quote\' inside of a single quote'

print[example1]
print[example2]

Output:

This is a "double quote" inside of a double quote
This is a 'single quote' inside of a single quote

How to Use a Backslash in a String Then

In Python, the backslash is a special character that makes escaping strings possible.

But this also means you cannot use it normally in a string.

For example:

print["This\is\a\test"]

Output:

This\is est

To include a backslash in a string, escape it with another backslash. This means writing a double backslash [\\].

For example:

print["This\\is\\a\\test"]

Output:

This\is\a\test

Conclusion

Today you learned how to quote a string in Python.

Thanks for reading. I hope you enjoy it!

Happy coding!

Further Reading

Python Double Quote vs Single Quote

Useful Advanced Features of Python

How do you add single quotes to a string?

Alternate between single and double quotes. For example, to add double quotes to a string, wrap the string in single quotes. To add single quotes to a string, wrap the string in double quotes.

How do you write a single quote in Python?

Python accepts single ['], double ["] and triple [''' or """] quotes to denote string literals, as long as the same type of quote starts and ends the string. word = 'word' sentence = "This is a sentence." paragraph = """This is a paragraph. It is made up of multiple lines and sentences."""

Can you use single quotes for string?

Single-quoted Strings: It is the easiest way to define a string. You can use it when you want the string to be exactly as it is written. All the escape sequences like \r or \n, will be output as specified instead of having any special meaning. Single-quote is usually faster in some cases.

How do you put quotation marks in a string?

Within a character string, to represent a single quotation mark or apostrophe, use two single quotation marks. [In other words, a single quotation mark is the escape character for a single quotation mark.] A double quotation mark does not need an escape character.

Chủ Đề