
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Different Types of Quotes in Python
In Python, strings can be defined using single quotes ('), double quotes ("), or triple quotes (''' or """). For example, 'Hello' is a valid string.
There are different types of quotes in Python. Each quotation is used in different scenarios as per the requirement. The following are the different types of quotations that we can use in the Python programming language.
-
Single quotes
-
Double quotes
-
Triple quotes
What are Single quotes?
Single quotes (' ') are used to create strings and characters in Python programming. Since the print() function accepts string values, we can directly pass values to it, wrapping them within the single quotes.
Example
In this example, we will create a string using single quotes and assign it to the variable before printing the output.
s = 'Welcome to Tutorialspoint' print(s) print(type(s))
Following is the output of the above code -
Welcome to Tutorialspoint <class 'str'>
Example
To include single quotes inside a string that's also enclosed in single quotes, escape characters (') are used. This tells Python to treat the single quote as a regular character and not as the end of the string.
s = 'It's Roshan's laptop' print(s) print(type(s))
Following is the output of the above code -
It's Roshan's laptop <class 'str'>
Example
In here, we are directly passing the string value to the print() function -
print('Python Programming Language')
Following is the output of the above code -
Python Programming Language
Example - Escape characters
In Python, when we use escape characters within quotes, they affect the string by performing special functions. To prevent this behavior and treat backslashes as literal characters, we use a raw string by prefixing the string with r.
my_str="c:\doc\navin" print("Without raw string-") print(my_str) print("Using raw string -") print(r'c:\doc\navin')
Following is the output of the above code -
Without raw string- c:\doc avin Using raw string - c:\doc\navin /home/cg/root/78531/main.py:1: SyntaxWarning: invalid escape sequence '\d' my_str="c:\doc\navin"
Example - quotes within quotes
When using quotes inside a string, the quotes used inside must be different from the ones used to define the string. This prevents syntax errors and helps Python interpret the string correctly.
s='"Hello, "welcome" to Tutorialspoint"' print(s)
Following is the output of the above code -
"Hello, "welcome" to Tutorialspoint"
What are Double quotes?
We can also use the Double quotes (" ") to create strings and characters. The functionality of single (') and double (") quotes is the same; you can use either one depending on your requirements.
Example
In this example, we are creating a string value using the double quotes.
st = "Double quotes in python programming" print(st) print(type(st))
Following is the output of the above code -
Double quotes in python programming <class 'str'>
Example
If we want to print a statement, we can also directly pass the desired text, placing it within double quotes, to the print() function.
print("Double quotes used in print statement")
Following is the output of the above code -
Double quotes used in print statement
What are Triple quotes?
The Triple quotes are used for commenting and representing the docString in Python.
Example
In a part of the code, if we want to use the sentences that are not part of code lines, then we will pass those sentences in the triple quotes. The following is the code.
string = "Hello world" '''The triple quotes are mainly used for commenting the lines in python programming language''' print(string)
The following is the output of the triple quotes used for commenting on the lines. In the output, we can see that the lines given in the triple quotes are not displayed in the output.
Hello world
Example
Instead of the triple quotes, we can also use the triple double quotes for commenting on the lines in the code.
s = "Hello world" """The triple quotes are mainly used for commenting the lines in python programming language""" print(s)
Following is the output of the above code -
Hello world