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
What are different types of quotes in Python?
In Python, strings can be defined using single quotes ('), double quotes ("), or triple quotes (''' or """). Each type serves different purposes and handles various scenarios like quotes within strings, multiline text, and escape characters.
Python supports three types of quotations for creating strings ?
-
Single quotes (') ? For basic strings and when double quotes appear inside
-
Double quotes (") ? For basic strings and when single quotes appear inside
-
Triple quotes (''' or """) ? For multiline strings and docstrings
Single Quotes
Single quotes (' ') are used to create strings in Python. They work identically to double quotes for simple string creation.
Basic Usage
Here's how to create a string using single quotes ?
name = 'Welcome to Tutorialspoint' print(name) print(type(name))
The output of the above code is ?
Welcome to Tutorialspoint <class 'str'>
Escaping Single Quotes
To include single quotes inside a single-quoted string, use escape characters (') ?
message = 'It's Roshan's laptop' print(message)
It's Roshan's laptop
Raw Strings
Raw strings (prefixed with 'r') treat backslashes as literal characters ?
path = r'c:\docs\newfile.txt'
print("Raw string:", path)
# Without raw string
normal_path = 'c:\docs\newfile.txt'
print("Normal string:", normal_path)
Raw string: c:\docs\newfile.txt Normal string: c:\docs\newfile.txt
Double Quotes
Double quotes (" ") work identically to single quotes but are preferred when the string contains single quotes.
Basic Usage
text = "Double quotes in Python programming" print(text) print(type(text))
Double quotes in Python programming <class 'str'>
Mixing Quote Types
Use double quotes when your string contains single quotes ?
sentence = "Python's syntax is clean and readable" print(sentence) # Alternatively, use single quotes with double quotes inside quote = 'He said "Hello World" to everyone' print(quote)
Python's syntax is clean and readable He said "Hello World" to everyone
Triple Quotes
Triple quotes (''' or """) are used for multiline strings, docstrings, and comments that span multiple lines.
Multiline Strings
multiline_text = """This is a multiline string that spans across multiple lines without needing escape characters""" print(multiline_text)
This is a multiline string that spans across multiple lines without needing escape characters
Comments and Documentation
def greet(name):
"""
This function greets a person with their name.
Args:
name (str): The name of the person to greet
Returns:
str: A greeting message
"""
return f"Hello, {name}!"
# Triple quotes for multi-line comments
'''
This is a multi-line comment
that explains the code below
'''
result = greet("Alice")
print(result)
Hello, Alice!
Comparison
| Quote Type | Use Case | Example |
|---|---|---|
| Single quotes (') | Basic strings, contains double quotes | 'He said "Hello"' |
| Double quotes (") | Basic strings, contains single quotes | "Python's features" |
| Triple quotes (''' or """) | Multiline strings, docstrings | """Line 1 Line 2""" |
Conclusion
Python's flexible quote system allows you to choose the most appropriate quote type based on your string content. Use single or double quotes for simple strings, and triple quotes for multiline text and documentation.
