Python Quotes
Python Quotes Or Quotations: In Python, when we created strings, we used quotes (“”). A string is literal text, which is surrounded by quotes. For example, “Hello Python!” now this is a string in Python. Strings is a data type in Python. Here Hello Python is just a simple text; we surrounded this text with quotes, and that’s making it a string.
In simple words, “String is a programming term that means a series of characters.” We used quotes only with strings, or we can say if we use quotes, that means the value is strings.
Now in the upper example, when we write “Hello Python,” we used double quotes. So which types of quotes can we use in Python? Single, double, or triple?
Types of Quotes in Python
In Python, we can use single (' ')
, double (" ")
, and triple (
”’ ”’)
quotes according to our uses. Single and double quotes are the same; we can use any of them. Triple quotes are used when we want to create multiline strings.
Single Quotes in Python
We can use single quotes when we want to create strings in Python.
Code:
print('Hello Python')
Output:
Hello Python
So in this example, we used single quotes to create strings. We can check the data type by using the type() function.
Code:
string_text = 'Hello Python'
print(string_text)
print(type(string_text))
Output:
Hello Python
<class 'str'>
Here ‘str’ class represents string data type.
Double Quotes in Python
We can use double quotes in Python to create strings.
Code:
string_text = "Hello Python"
print(string_text)
Output:
Hello Python
Single quotes and double quotes are the same in Python. Sometimes it’s required to use specific quotes; otherwise, it’s just a matter of choice.
Triple Quotes in Python
We use triple quotes when we want to create multiline strings. For example:
Code:
print('Rain rain')
print('Go away')
print('Come again')
print('Another day')
Output:
Rain rain
Go away
Come again
Another day
So instead of using single quotes four times, we can use triple quotes for multiline strings.
Code:
print("""Rain rain
Go away
Come again
Another day""")
Output:
Rain rain
Go away
Come again
Another day
In the upper example, we used triple double-quotes; if we want, we can use triple single-quotes.
Code:
print('''Rain rain
Go away
Come again
Another day''')
Output:
Rain rain
Go away
Come again
Another day
Output is the same. Also, if we want, we can use triple quotes in single-line strings.
Code:
print('''Hello Python''')
print("""Hello Python""")
Output:
Hello Python
Hello Python
Click to learn about ‘using quotes’ from python.org
Rules for Using Quotes in Python
There are some basic rules that we should follow when we use quotes in Python.
Open and Close Quotes Should be Same
If we use double quotes in strings, then strings should have double quotes on both sides (open and close). For example:
Code:
info = "Learning Python'
Output:
SyntaxError: unterminated string literal
We get SyntaxError; it says unterminated string. It means we didn’t terminate our string, which means we didn’t close our string. However, we terminate our string. But we open our string with double quotes and close it with single quotes. So interpreter doesn’t count this string as terminated string. So if we open it with double quotes, then we should close it with double quotes.

Same way if we write like:
Code:
info = 'Learning Python"
This will produce the same SyntaxError. Now here is one interesting practical:
Code:
info = 'Learning' "Python"
print(info)
Output:
LearningPython
Now what we did here is divide our string into two parts. And we follow the rules. The first part (learning) starts and ends with single quotes. And the second part (Python) starts and ends with double quotes. So we get output without any error.
So this is the basic rule: open and close quotes should be the same.
When Using Quotes Inside Quotes
Now suppose we want text: No ‘Game’ Today as output. Now how do we write it? For example:
Code:
info = 'No 'Game' Today'
print(info)
Output:
SyntaxError: invalid syntax
We get SyntaxError because this is not the right way to use quote when we want a quote word (‘Game’) in output. It just looks like we are diving strings into parts. Just like we did in the upper example. In this case, we used double quotes.
Code:
info = "No 'Game' Today"
print(info)
Output:
No 'Game' Today
This is the correct way. We open and close these strings with double quotes. When we open with double quotes, the interpreter expects to be closed with double quotes. And everything we write between double quotes is considered normal text.
Same if we want to text: No “Game” Today in output. We use single quotes to start and end with a string.
Code:
info = 'No "Game" Today'
print(info)
Output:
No "Game" Today
It has same explanation. We start with single quotes, so the interpreter expected to be closed with single quotes, and between those quotes, whatever we wrote is valid text.
However, in such a situation, we can use triple quotes too.
Code:
info = '''No "Game" Today'''
info1 = """No 'Game' Today"""
print(info)
print(info1)
Output:
No "Game" Today
No 'Game' Today
And same if we want triple quotes in the result. Then we can use either single or double quotes. For example:
Code:
info = """No '''Game''' Today"""
info1 = '''No """Game""" Today'''
print(info)
print(info1)
Output:
No '''Game''' Today
No """Game""" Today
Only Strings Use Quotes
Python had different kinds of data types, but only the string data type uses quotes. Or we can say if we are using quotes, that means we are creating a string. For example:
Code:
num1 = 1000
num2 = '1000'
num3 = 5.5
num4 = '5.5'
print(type(num1))
print(type(num2))
print(type(num3))
print(type(num4))
Output:
<class 'int'>
<class 'str'>
<class 'float'>
<class 'str'>
Here num1 is an ‘int’ class data type, but when we used the same value (1000) with quotes,. It’s changed in strings. So now num2 is ‘str’ class. Same goes with num3. num3 is a ‘float’ class data type, but when we used the same value with quotes (5.5), it’s changed in strings. So now num4 is ‘str’ class. So always remember, only the string data type uses quotes in Python.
So these are some basic rules that we should consider when using quotes in Python.
Conclusion
So this is the end of the Python quotes tutorial. Quotes are required when we want to create a string data type. Any kind of quote we can use according to our requirement. So these are some basic rules to use quotes in Python.
End of Python Quotes Tutorial
5 thoughts on “Python Quotes: What Are Different Types of Quotes in Python?”