SoFunction
Updated on 2025-04-14

Python's multiple ways to create multi-line strings

1. Use three quotes ('''  or """")

This is the most commonly used method, you can use three single quotes (''') or three double quotes (""") to create multi-line strings. In this way, the newlines in the string will be preserved.

1.1 Example

# Use three single quotesmulti_line_string = '''This is a multi-line string.
It spans multiple lines.
Each line is separated by a newline character (\n).'''
 
print(multi_line_string)
This is a multi-line string.
It spans multiple lines.
Each line is separated by a newline character (\n).
# Use triple double quotesmulti_line_string = """This is another multi-line string.
It also spans multiple lines.
Each line is separated by a newline character (\n)."""
 
print(multi_line_string)

Output:

This is another multi-line string.
It also spans multiple lines.
Each line is separated by a newline character (\n).

2. Use backslash (\) to continue

Although this method is not as intuitive as triple quotes, it can also be used to create multi-line strings. By using backslashes at the end of the line\, multiple lines of code can be merged into one line.

2.1 Example

multi_line_string = 'This is a multi-line string. \
It spans multiple lines. \
Each line is separated by a newline character (\n).'
 
print(multi_line_string)

Output:

This is a multi-line string. It spans multiple lines. Each line is separated by a newline character (\n).

3. Use brackets ( ) to continue

Strings in parentheses can be written across multiple lines, and Python will automatically merge them into a string.

3.1 Example

multi_line_string = ('This is a multi-line string. '
                     'It spans multiple lines. '
                     'Each line is separated by a newline character (\n).')
 
print(multi_line_string)

Output:

This is a multi-line string. It spans multiple lines. Each line is separated by a newline character (\n).

4. Use list or tuple stitching

Although this is not a way to create multi-line strings directly, similar effects can be achieved by splicing multiple strings.

4.1 Example

lines = [
    'This is a multi-line string.',
    'It spans multiple lines.',
    'Each line is separated by a newline character (\n).'
]
 
multi_line_string = '\n'.join(lines)
 
print(multi_line_string)

Output:

This is a multi-line string.
It spans multiple lines.
Each line is separated by a newline character (\n).

5. Use suggestions and precautions in actual development

5.1 Document string

Multi-line strings are often used to define document strings for functions or classes. Document strings should clearly and concisely describe the functions and usage of functions or classes.

def greet(name):
    """
    This function greets the person passed as a parameter.
    Parameters:
    name (str): The name of the person to greet.
    Returns:
    str: A greeting message.
    """
    return f"Hello, {name}!"
 
print(greet.__doc__)

Output:

This function greets the person passed as a parameter.
 
Parameters:
name (str): The name of the person to greet.
 
Returns:
str: A greeting message.

5.2 Configuration files and templates

Multi-line strings can conveniently represent complex text structures when processing configuration files or generating HTML templates.

html_template = '''
<!DOCTYPE html>
<html>
<head>
    <title>Page Title</title>
</head>
<body>
    <h1>Welcome to My Website</h1>
    <p>This is a paragraph.</p>
</body>
</html>
'''
 
print(html_template)

Output:

<!DOCTYPE html>
<html>
<head>
    <title>Page Title</title>
</head>
<body>
    <h1>Welcome to My Website</h1>
    <p>This is a paragraph.</p>
</body>
</html>

5.3 Pay attention to indentation

When you create a multiline string with three quotes, the indentation in the string is preserved. If you do not need to retain indentation, you can useFunction removes unnecessary indentation.

import textwrap
 
multi_line_string = ('''\
    This is a multi-line string.
    It spans multiple lines.
    Each line is separated by a newline character (\n).''')
 
print(multi_line_string)

Output:

This is a multi-line string.
It spans multiple lines.
Each line is separated by a newline character (\n).

5.4 Avoid unnecessary escape characters

In multi-line strings, there is usually no need to escape quotes unless you need to include the three quotes themselves in the string.

multi_line_string = '''This is a multi-line string with "double quotes" and 'single quotes'.
It spans multiple lines.
Each line is separated by a newline character (\n).'''
 
print(multi_line_string)

Output:

This is a multi-line string with "double quotes" and 'single quotes'.
It spans multiple lines.
Each line is separated by a newline character (\n).

There are many ways to create multiline strings in Python, the most commonly used is three quotes ('''or"""). Understanding these methods and choosing the right way according to specific needs can improve the readability and maintenance of the code.

In actual development, paying attention to indentation, escaped characters and string splicing can make the code more robust and efficient.

This is the end of this article about various methods for creating multi-line strings in Python. For more related content on creating multi-line strings in Python, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!