When to use single vs double quotes?



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


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

FAQ: Programming with Python - Learn Python: Syntax - Strings