
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
What is the difference between single and double quotes in python?
Python uses quotes to represent string objects. They can either be a single quote or double quotes. Both ways are correct and work the same way; however, the difference occurs when these quotes are used together.
In this article, we will learn the difference between single quotes and double quotes.
Single Quotes in Python
Single quotes should be used to wrap small and short strings in Python, such as string literals or identifiers. You must remember that using single quotes as a character of a string, while representing the string with single quotes, will raise an error. In such cases, using double quotes is recommended. Let us understand it through an example.
Example
In the following example, we will represent multiple type of strings: a single word, multiple words and multiple sentences.
name = 'Rahul' print(name) channel = 'Better Data Science' print(channel) paragraph = 'Rahul enjoys the content on Better Data Science. Rahul is awesome. Think like Rahul.' print(paragraph) hybrid = 'Hello "World"!' print(hybrid)
Output
If we execute the program above, the output is given below −
Rahul Better Data Science Rahul enjoys the content on Better Data Science. Rahul is awesome. Think like Rahul. Hello "World"!
Example
Let us see another program below; using multiple single quotes in a single string.
hybrid = 'Rahul's World' print(hybrid)
Output
The program raises a syntactical error as shown below −
File "/home/cg/root/96099/main.py", line 1 hybrid = 'Rahul's World' ^ SyntaxError: unterminated string literal (detected at line 1)
Python assumes the string ends after “Rahul”, so anything after that is a syntactical error. In code editors, errors like this are easily identified since the area after We is colored differently.
The ways to get rid of this problem are
Stop using contractions (we are -> we're) - they're inconvenient.
Escape a string - This is an option we'll look into next.
Use double quotation marks.
Escape a string
The basic goal of escaping a string is to prevent particular characters from being utilized in the computer language. We don't want the apostrophe to be considered as a quote mark, for example.
Example
To escape a string character in Python, use the backslash (\) sign
hybrid = 'Rahul's World' print(hybrid)
Output
The output for this program above is as follows −
Rahul's World
Example
However, backslash is frequently used as a literal character in strings, such as to represent the path a computer. Let’s see what will happen if you try to print a path with the escape character.
print('C:\Users\John')
Output
If we compile and run the program above, a syntax error is raised −
C:\Users\John C:\Users\John
Probably not what you were hoping to see. As it turns out, there are two ways to get away from the escape character −
Write ‘r’ before the first quote mark if you're using a raw string.
Use a double backslash to effectively escape the escape character.
Here's how to perform both −
Example
#Write r before the first quote mark if you're using a raw string print(r'C:\Users\John') #Use a double backslash to effectively escape the escape character print('C:\Users\John')
Output
If we execute the program above, the output is shown as follows −
C:\Users\John C:\Users\John
These two rules apply to strings wrapped by single and double quotations. Let us now discuss the usage of double quotes in strings, further in this chapter.
Double Quotes in Python
Double quotes are recommended for natural language communications, string interpolations, or when you know there will be single quotes within the string. Let us understand better using examples below.
Example
Let us look at various cases in which double quotes can be used to represent strings in Python, in the following example.
name = 'Rahul' # Natural language print("It is easy for us to get confused with the single and double quotes in Python.") # String interpolation print(f"{name} said he is free today.") # No need to escape a character print("We're going out today.") # Quotation inside a string print("my favourite subject is 'maths'")
Output
If we compile and run the program above, the output is produced as follows −
It is easy for us to get confused with the single and double quotes in Python. Rahul said he is free today. We're going out today. my favourite subject is 'maths'
As you can see, embedding quotations into strings surrounding by double quotation marks is simple. There's also no need to escape a character like we did with single quotes.
Example
Remember that you cannot use double quotes again in a string that is enclosed by double quotations. This will cause the same syntactic issue as single quotes. Let us see it in the example below.
string = "He said, "I can't handle this anymore"." print(string)
Output
The output for the program above is as follows −
File "/home/cg/root/22693/main.py", line 1 string = "He said, "I can't handle this anymore"." ^ SyntaxError: unterminated string literal (detected at line 1)
Example
To avoid this, you can apply the previous section's approach, but you can instead surround the string in single quotes −
string = 'He said, "I cannot handle this anymore".' print(string)
Output
The output is produced as follows −
He said, "I cannot handle this anymore".
Conclusion
In Python, the difference between single and double quoted strings is negligible. You can use either one for anything as long as you follow your programming conventions. There are some circumstances where one type has an advantage over the other.
- Related Articles
- What is the difference between single and double quotes in JavaScript?
- Bash Single vs. Double Quotes What\'s Difference
- Single and Double Quotes in Perl
- Single quotes vs. double quotes in C or C++\n
- Difference between single quote (‘) and double quote (“) in PowerShell?
- Difference between Single Action and Double Action Guns
- Is there a PHP function that only adds slashes to double quotes NOT single quotes
- Difference between Single-Cage and Double-Cage Induction Motors
- Write the difference between single entry system and double entry system.
- What Is Double Dot (..) And Single Dot (.) In Linux?
- How to print double quotes with the string variable in Python?
- What is the difference between a float, double and a decimal in C#?
- What is the difference between single-line and multi-line comments in JavaScript?
- Difference Between Float and Double
- What is the difference between = and == operators in Python?
