| by Arround The Web | No comments

Python Multiline String

The term “multiline string” refers to a string that consists of multiple lines. In Python, strings are enclosed in quotes, and the newline character “\n” is used to indicate a new line. However, when dealing with a large block of text that spans multiple lines, it can become cumbersome to include “\n” at the end of each line. This is where a multiline string comes in handy.

This article presents a complete overview of Python’s “multiline string” using numerous examples.

How to Create a Multiline String in Python?

Python provides a variety of methods for creating multiline strings. Some of the methods are given below:

Method 1: Create a Multiline String in Python Using “Triple Quotes”

The “Triple Quotes” allow us to create multiple-line strings without having to use escape characters. This method can be utilized to make/create a “multiline string” in Python. To do so, simply enclose the provided string in triple quotes.

Example:

Let’s overview of the following example:

multiline_string = '''Hello and

Welcome to

Python Guide

'''

print(multiline_string)

In the above code, the variable named “multiline_string” creates a multiline string that spans across three lines via the “Triple Quotes”, as discussed. After that, print the stated multiline string.

Output

In the above output, the multiline string has been created and displayed appropriately.

Note: Python preserves all the whitespace characters, including the newline character “\n” and any spaces or tabs that may be present at the beginning of each line. This can be useful when formatting text, however, if you’re not careful, it can also result in problems.

For example, consider the following multiline string:

multiline_string = '''

Hello and

Welcome to

Python Guide

'''

print(multiline_string)

Here, we have added some indentation at the beginning of each line, using spaces. If we were to print this string, we would see that the indentation is preserved.

Output

In the above output, the multiline string with some indentation has been created, thereby preserving the indentation.

Method 2: Create a Multiline String in Python Using “Escape Characters”

Escape Characters” are special characters that are used to represent certain characters that cannot be directly represented in a string. These characters can also be utilized to generate a multiline string in Python.

Example

Here’s an example code:

multiline_string = 'Hello and\nWelcome to \nPython\nGuide'

print(multiline_string)

According to the above code snippet, we have used the escape character “\n” to represent a newline character. By using this character, we can create a string that spans multiple lines i.e., a “multiline string”.

Output

In the above output, the multiline string has been created.

Method 3: Create a Multiline String in Python Using the “join()” Method

The “join()” method is used to join a sequence of strings together to create one long string. This approach can also be utilized to generate a multiline string in Python.

Syntax

string.join(iterable)

In the above syntax, “iterable” is any sequence or collection that supports iteration, such as a list, a string, a tuple, a dictionary, etc.

Example

Here’s an example code:

lines = ['Hello', 'and', 'Welcome to', 'Python Guide']

multiline_string = '\n'.join(lines)

print(multiline_string)

In this example, we have created a string list and used the “join()” method to join them together into one string. We have used the newline character “\n” as the separator between the strings.

Output

The above output implies that the multiline string has been created via the “join()” method.

Conclusion

To create a multiline string in Python, various approaches such as “Triple Quotes”, “Escape Characters”, or the “join()” method can be used. The string values containing more than one line are enclosed by “Triple Quotes” to create a multiline string without using escape characters such as “\n”. But we can also use escape characters such as “\n” to create the multiline string. This post demonstrated the concept of multiline strings and different methods to create it(multiline string) in Python using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply