Python Multiline String

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The python multiline string can be implemented in various ways. We can use triple quotes, brackets, and backlash to define multi-line strings. The triple quotes style is one of the simplest and most commonly used ways to divide a large string into a python multiline string.

Introduction

Before learning about the python multiline string, and examples, let us first learn about the strings in python.

A string is a sequence of characters. The string is a derived immutable (once defined, they cannot be changed) data type in python. We can use Python methods, such as replace, join, or split, to modify the strings.

There are several ways of defining strings in python. We can use single quotes (' '), double quotes (" "), and triple quotes (''' ''').

Now, let us learn about the python multiline string. The python multiline string is used when the string is very long in size. It is used to keep text in a single line, but it affects the overall readability of the code.

Create a Python Multiline String with Examples

The python multiline string can be implemented in various ways. We can use triple quotes, brackets, and backlash to define multi-line strings. Let us take examples to understand the python multiline string better.

Using triple quotes

The triple quotes style is one of the simplest and most commonly used ways to divide a large string into a python multiline string. Refer to the examples below to know how to use them.

Example 1:

Output:

Example 2:

Output:

To define the python multiline string, enclose the string with a pair of Triple quotes, one at the start and the second at the end.

Using parentheses and single/double quotes

We can also define a python multiline string by enclosing the parts of the multiline string using brackets. As shown above, we can use both single quotes (' ') and double quotes (" "). Refer to the examples below to know how to use them.

Example 1:

Since there was no delimiter or newline character (\n) in the original string, the output will be:

Output:

Example (2):

Output:

Now, we can use the newline character or \n to separate the string. See the example below for a better understanding.

Example:

Output:

Using \

There is still one more way of handing the python multiline string. We can use a backslash (\) to join strings on multiple lines. The backslash way is one of the least used ways to maintain line continuation. Refer to the examples below to know how to use them.

Example:

Output:

As we can see, since there was no delimiter or newline character (\n) in the original string, the strings came out one after the other.

Now let us use the new line character to separate our string.

Example:

Output:

Conclusion

  • The python multiline string can be implemented in various ways. We can use triple quotes, brackets, and backlash to define multi-line strings.
  • The triple quotes style is one of the simplest and most commonly used ways to divide a large string into python multiline strings.
  • A python multiline string can also be defined by enclosing the parts of the multiline string using brackets.
  • We can also use a backslash (\) to join strings on multiple lines. The backslash way is one of the least used ways to maintain line continuation.

See Also: