Triple Quotes in Python

Python's triple quotes allow strings to span multiple lines, including verbatim newlines, tabs, and any other special characters. This feature is particularly useful for multi-line strings, docstrings, and preserving text formatting.

The syntax for triple quotes consists of three consecutive single (''') or double (""") quotes.

Syntax

# Using triple double quotes
text = """Multi-line
string content"""

# Using triple single quotes  
text = '''Multi-line
string content'''

Basic Multi-line String Example

Triple quotes preserve all whitespace and special characters within the string ?

para_str = """This is a long string that is made up of
several lines and non-printable characters such as 
TAB (\t) and they will show up that way when displayed.
NEWLINEs within the string, whether explicitly given 
like this within the brackets [\n], or just a NEWLINE 
within the variable assignment will also show up."""

print(para_str)
This is a long string that is made up of
several lines and non-printable characters such as 
TAB (	) and they will show up that way when displayed.
NEWLINEs within the string, whether explicitly given 
like this within the brackets [
], or just a NEWLINE 
within the variable assignment will also show up.

Using Triple Quotes for Docstrings

Triple quotes are commonly used for function and class documentation ?

def calculate_area(length, width):
    """
    Calculate the area of a rectangle.
    
    Args:
        length: The length of the rectangle
        width: The width of the rectangle
        
    Returns:
        The area as length * width
    """
    return length * width

print(calculate_area.__doc__)

    Calculate the area of a rectangle.
    
    Args:
        length: The length of the rectangle
        width: The width of the rectangle
        
    Returns:
        The area as length * width
    

Triple Quotes vs Raw Strings

Raw strings treat backslashes as literal characters, not escape sequences ?

# Regular string - backslash is treated as escape character
normal_path = "C:\Users\Documents"
print("Normal string:", normal_path)

# Raw string - backslash is treated literally
raw_path = r"C:\Users\Documents"  
print("Raw string:", raw_path)

# Triple quote with raw string
triple_raw = r"""C:\Users\Documents\
file.txt"""
print("Triple raw:", triple_raw)
Normal string: C:\Users\Documents
Raw string: C:\Users\Documents
Triple raw: C:\Users\Documents\file.txt

Common Use Cases

Use Case Example Benefit
Multi-line text """Line 1\nLine 2""" Preserves formatting
Docstrings """Function docs""" Standard documentation
SQL queries """SELECT * FROM table""" Readable queries
HTML templates """<div>content</div>""" Preserve HTML structure

Practical Example

# Creating a formatted email template
email_template = """
Dear {name},

Thank you for your purchase of {product}.
Your order total: ${amount}

Best regards,
Customer Service Team
"""

# Using the template
message = email_template.format(
    name="John Doe",
    product="Python Course", 
    amount="99.99"
)

print(message)

Dear John Doe,

Thank you for your purchase of Python Course.
Your order total: $99.99

Best regards,
Customer Service Team

Conclusion

Triple quotes are essential for multi-line strings, docstrings, and preserving text formatting in Python. Use them when you need strings that span multiple lines or contain complex formatting that would be difficult to achieve with regular quotes.

Updated on: 2026-03-25T07:33:01+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements