Multiline Strings in Python

Multiline Strings in Python Code

In Python, multiline strings can be created using triple quotes, either triple single quotes (''') or triple double quotes ("""). These strings can span multiple lines, which makes them useful for including long texts or documentation. Here's a brief overview:

Creating Multiline Strings

Using Triple Double Quotes

Python Code

multiline_string = """This is a multiline string.

It spans multiple lines.

You can include "quotes" and other characters."""

Using Triple Single Quotes

Python Code

multiline_string = '''This is another multiline string.

It also spans multiple lines.

You can include 'quotes' and other characters.'''

Preserving Whitespace and Newlines

Multiline strings preserve all whitespace and newlines exactly as they are written.

Python Code

multiline_string = """

Line 1

Line 2

Indented Line

Line 4

"""

print(multiline_string)

Output:

mathematica

Line 1

Line 2

Indented Line

Line 4

Escaping Characters

Within a multiline string, you can still use escape sequences like \n for new lines, \t for tabs, etc.

Python Code

multiline_string = """This is a multiline string with an escaped newline character:\nThis appears on a new line."""

print(multiline_string)

Output:

csharp

This is a multiline string with an escaped newline character:

This appears on a new line.

Example: Multiline String in a Function

Multiline strings are often used in functions to provide detailed docstrings.

Python Code

def example_function():

"""

This is an example function.

It demonstrates how to use a multiline string as a docstring.

Parameters:

None

Returns:

None

"""

pass

Concatenating Multiline Strings

You can concatenate multiline strings using the + operator or by placing them adjacent to each other.

Python Code

string_part1 = """This is the first part of a multiline string. """

string_part2 = """This is the second part."""

full_string = string_part1 + string_part2

print(full_string)

Output:

csharp

This is the first part of a multiline string. This is the second part.

Using Multiline Strings for Text Blocks

Multiline strings are very useful for representing text blocks, such as HTML templates, SQL queries, or any large piece of text.

Python Code

html_template = """

<html>

<head>

<title>Title</title>

</head>

<body>

<h1>Hello, World!</h1>

<p>This is a multiline string example.</p>

</body>

</html>

"""

print(html_template)

Raw Multiline Strings

To avoid escaping characters, you can use raw string notation by prefixing the string with r.

Python Code

raw_multiline_string = r"""This is a raw multiline string.

It will not process escape sequences like \n or \t."""

print(raw_multiline_string)

Output:

vbnet

This is a raw multiline string.

It will not process escape sequences like \n or \t.

Using multiline strings in Python is straightforward and enhances code readability, especially when dealing with large text blocks or detailed documentation.

Top of Form

Post a Comment

Previous Post Next Post