When to use single vs double quotes?

Question

In the context of this exercise which introduces strings, when should single or double quotes be used for a string?

Answer

Typically, either single or double quotes can be used interchangeably for creating a string. However, in certain situations, the type of quotes can matter, depending on the content of the string itself. If the string contains single quotes, it can be more convenient to wrap the entire string in double quotes. And if the string contains double quotes, you might use single quotes to wrap the string instead.

The reason for this is that each string starts and ends when a pair of quotes is encountered. If there is another matching quote within the string, that may end the string prematurely, causing errors.

# Examples of valid strings
print("I'm coding!")
print('The computer printed the string "Hello!"')

# Example of invalid string 
print("A common phrase in programming is "Hello world"")
41 Likes

There is no difference unless you have words like (It’s, they’re, etc…)
but you can always use '.

# EXAMPLE

# the following two lines print Hello World! to the screen
print('Hello World!')
print("Hello World")

# the following two lines prints It's a sunny day
print('It\'s a sunny day')
print("It's a sunny day")
69 Likes

What if you had to use two types of quotes inside of a string? For instance if I wrote (example: Bobbi told me, “Delia said, ‘This will never work.’ ”)?

12 Likes

Well, you could use triple-quotes, but that would (IMHO) be a misuse of triple-quotes. I think that the Pythonic solution would be to escape (backslash) the double-quotes:

print(""" "Delia said, 'This will never work.' " """)  # works, but ugly

print(" \"Delia said, 'This will never work.' \" ")   # better, I think

Output:

 "Delia said, 'This will never work.' " 
 "Delia said, 'This will never work.' " 
39 Likes

I did’t get it at first, and thank you so much. To be honest this Codecademy thing is actually quite moving

24 Likes

what is the difference between a double and a single quoted string.

In Python, there’s no difference.

3 Likes

Thank you, I did not know that using “” or ’ made a difference with special characters.

One thing to consider is if moving into other languages, there is a distinction between " and '. I’m still new myself, but also wanted to develop good habits with my choice on this. I know I want to get into C# later on, and so I’m going to default to using " instead of '. Just something to think about for building a habit.

8 Likes

How about using triple quotes in print to allow the use of single as well as double quotes within the same sentence?? Isn’t that easier for beginners??

oh so it’s not like C++ where you use ’ ’ for a char value and “” for a string

Hello @objectsurfer90671 and welcome to the Codecademy Forums!

Yes, in Python, there is no difference between using ' or " to surround a string. This is because Python does not have a char type and any characters are just strings with a length of 1.

1 Like

Thanks for example but In Previous examples someone was saying that we have to use escape sequence

Like this:
print(" I'm coding ")

So what is the difference between above code and this code?

That would not be an escape, see the earlier response for using escape in strings-

You can use the \ backslash escape character to use escape sequences e.g. "\n" for a newline or to escape special characters such as quotes so that they are printed instead of being interpreted, e.g.

print('Here\'s an example')

For the given example we don’t have to use \ and we can instead use different quotes to create our string literal (as per the original example) but when you’re mixing the two using escape may be easier.

Practically you’d be looking to make it as readable as possible and ideally to be consistent in style with the rest of the project.

2 Likes

advantage to using double quotes when you are creating a print call with words or sentences is that it would eliminate some of the issues if you had an apostrophe in your printed quotes python will think you are ending your quote there not using it as an apostrophe

2 Likes

Have I understood this correctly, the \" is a special character that tells python to print render a " instead of assuming that it is a close of the original string in the print function?

It’s not a special character itself, it’s just interpreted to mean something different, in this example yes instead of closing the string literal it’s interpreted as a ". More generally this is known as an escape sequence which is replaced by one or more actual characters inside the actual string. You might already be familiar with one or two like \n creating newlines or \t inserting tab characters.

So it’s a character replacement that’s separate to the actual printing (exactly how the characters are represented when printed is platform dependent).

There are some details about the possible escape sequences at the following (scroll down a bit for a table)
https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals

You should use "s when you want to use 's in your string and vice versa. although you can backslash.

1 Like

What do you mean by:

  • The reason for this is that each string starts and ends when a pair of quotes is encountered.

Do you mean two quotes at the end, or a double-quote?
Exactly when does a string end?
Thanks
Arthur

What are some common mistakes or pitfalls that new coders should watch out for when using quotes or defining strings in their code? Are there any best practices or tips that you would recommend for avoiding these mistakes?