How do i put single quotes in 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).

A computer program looks like a code language, which is necessary for the computer to precisely understand what your commands mean. But, being a code language makes it harder for humans to read. To compensate for this, you are allowed to write extra notes in your program that the computer ignores. These notes are called comments.

In Python, any line of instructions containing the # symbol ("pound sign" or "hash") denotes the start of a comment. The rest of the line will be ignored when the program is run. Here is an example.

Because the second line started with a # sign, Python totally ignored that line, and as you can see, the number 2 was not printed. Common uses for comments include:

  • explaining parts of the program, for you or other people to read later;
  • leaving "to do" notes, when you write a longer program;
  • temporarily disabling ("commenting out") a line of a program without totally deleting it, so that it is easier to put back in later.

Here is an exercise to illustrate. If you edit the code too much and want to bring back the default version of the code, select Reset code to default.

Strings

Strings are sequences of letters and numbers, or in other words, chunks of text. They are surrounded by two quotes for protection: for example in Lesson 0 the part "Hello, World!" of the first program was a string. If a pound sign # appears in a string, then it does not get treated as a comment:

This behaviour is because the part inside the quotes "" is a string literal, meaning that it should be literally copied and not interpreted as a command. Similarly, print("3 + 4") will not print the number 7, but just the string 3 + 4.

Escape Sequences

What if you want to include the quote character " inside of a string? If you try to execute print("I said "Wow!" to him") this causes an error: the problem is that Python sees one string "I said " followed by something Wow! which is not in the string. This is not what we intended!

Python does have two simple ways to put quote symbols in strings.

  • You are allowed to start and end a string literal with single quotes (also known as apostrophes), like 'blah blah'. Then, double quotes can go in between, such as 'I said "Wow!" to him.'
  • You can put a backslash character followed by a quote (\" or \'). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string. Here is an example.

Furthermore, because of escape sequences, backslash (\) is a special character. So to include a backslash in a string, you actually need to "escape it" with a second backslash, or in other words you need to write \\ in the string literal.

How do i put single quotes in a string in python?

Multiple Choice Exercise: Escape Characters

What is the output of print("Backslashes \\ and single quotes \' and double quotes \" and pound signs # are awesome!")

Your choice:

Correct!

There are other escape sequences, like "newline," that we won't discuss right now. For now, you are ready to move on to the next lesson!

How do you add a single quote to a string?

The following are our strings with single and double quote. String str1 = "This is Jack's mobile"; String str2 = "\"This is it\"!"; Above, for single quote, we have to mention it normally like. However, for double quotes, use the following and add a slash in the beginning as well as at the end.

Can you use single quotes in Python?

In Python, it's a best practice to use single quotes to surround small and short strings, such as string literals or identifiers. But here's the deal - it's not a requirement. You can write entire paragraphs and articles inside single quotes.

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 print a single quote in Python?

Use the escape character to print single and double quotes Use the escape character \ before double or single quotes to include them in the string.