triple-quoted string
In Python, a triple-quoted string is a string literal enclosed in a pair of triple quotes, either single ('''
) or double ("""
).
Triple-quoted strings can span multiple lines, making them useful for writing long strings, such as templates, docstrings, or string literals that contain line breaks. With triple-quoted strings, you don’t need to use escape characters for newlines, which can make your code more readable and maintainable.
You can also use triple-quoted strings to include both single and double quotes within the string without the need for escape characters. This feature comes in handy when you’re dealing with strings that contain complex text, such as HTML or JSON content, where quotes are common.
Example
Here’s a quick example of using a triple-quoted string in Python:
>>> triple_quoted = """This is a triple-quoted string.
... It can span multiple lines.
... You can include 'single' and "double" quotes without any issues."""
>>> print(triple_quoted)
This is a triple-quoted string.
It can span multiple lines.
You can include 'single' and "double" quotes without any issues.
In this example, you create a triple-quoted string using double quotes. This type of string literal can span multiple lines.
Related Resources
Tutorial
Strings and Character Data in Python
In this tutorial, you'll learn how to use Python's rich set of operators and functions for working with strings. You'll cover the basics of creating strings using literals and the str() function, applying string methods, using operators and built-in functions with strings, and more!
For additional information on related topics, take a look at the following resources: