Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Quotation in Python
Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as long as the same type of quote starts and ends the string.
Types of Quotes in Python
Python supports three types of quotation marks for creating strings. Each has its specific use cases and advantages.
Single Quotes
Single quotes are the most basic way to create strings in Python ?
word = 'Hello' name = 'Python' print(word) print(name)
Hello Python
Double Quotes
Double quotes work exactly like single quotes but are useful when your string contains apostrophes ?
sentence = "This is a sentence." message = "It's a beautiful day!" print(sentence) print(message)
This is a sentence. It's a beautiful day!
Triple Quotes
Triple quotes (either ''' or """) are used to span strings across multiple lines and are commonly used for docstrings ?
paragraph = """This is a paragraph. It is made up of multiple lines and sentences.""" poem = '''Roses are red, Violets are blue, Python is awesome, And so are you!''' print(paragraph) print() print(poem)
This is a paragraph. It is made up of multiple lines and sentences. Roses are red, Violets are blue, Python is awesome, And so are you!
When to Use Each Type
| Quote Type | Best For | Example Use Case |
|---|---|---|
| Single quotes ' | Simple strings | Variable names, short text |
| Double quotes " | Strings with apostrophes | Contractions like "don't" |
| Triple quotes ''' or """ | Multi-line strings | Docstrings, paragraphs |
Mixing Quotes
You can include different quote types within a string by using different outer quotes ?
# Using double quotes to include single quotes message1 = "He said, 'Hello World!'" # Using single quotes to include double quotes message2 = 'She replied, "How are you?"' print(message1) print(message2)
He said, 'Hello World!' She replied, "How are you?"
Conclusion
Python's flexible quoting system allows you to choose the most appropriate quote type for your strings. Use single or double quotes for simple strings, and triple quotes for multi-line text or docstrings.
