AppDividend
Latest Code Tutorials

How to Create Multiline String in Python

0

Python provides different ways to create a multiline string. It can be useful when you have a very long string, and you can divide that string into multiline. You shouldn’t keep such text in the single line because it quickly becomes tedious and kills the readability. When using PEP8 code checkers such as flake8 in Python, an error of E501 line too long occurs when one line exceeds 80 characters. Before proceeds further, let’s see some overview of Python string.

String Literals in Python

String literals are surrounded by either single quotation marks(”), or double quotation marks(“”). ‘Python’ is the same as “Python”. You can use one of the ways to create a string but stick with it throughout your project. If you want to display the string literal, then you can do it by using the print() function.

print('Single quote Python')
print("Double quote Python")

Output

Single quote Python
Double quote Python

Assigning String to a Variable

If you have to assign a string to a variable, then you can do with a variable name followed by the equal sign and a string.

data = 'Python'
print(data)

Output

Python

But what if our string is too long and does not fit in the one line.

data = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"
print(data)

If you face this kind of scenario, then you have to write the string in multiline. Users can’t read the above string in any standard way. It kills the readability. So. let’s learn the different ways to create multiline String in Python.

Different ways to create multiline String in Python

There are four ways that you can create a multiline String.

  1. Using triple quotes to create a multiline string.
  2. Using brackets to define a multiline String.
  3. Using backslash to entitle a multiline String.
  4. Using a join() method to define a multiline String.

Using triple quotes to create a multiline string

The triple quotes method is the simplest method to let a long string split into different lines. In this approach, you will need to enclose the string with a pair of triple quotes, one at the start and another at the end. That is it. The output will be as it is as you have split the lines in that long string.

# app.py

data = '''Lorem Ipsum is simply dummy text of the printing
and typesetting industry. Lorem Ipsum has 
been the industry's standard dummy text ever since the 1500s'''

print(data)

Output

Lorem Ipsum is simply dummy text of the printing
and typesetting industry. Lorem Ipsum has
been the industry's standard dummy text ever since the 1500s

If you write anything inside the enclosing Triple quotes, that will become a part of one multiline string. If your long string contains newline characters, then you should use the triple quotes to write them into multiple lines. One thing here to note that anything goes inside triple quotes is a string value, so if your long string has many newline characters, then you can use the newline characters to split the string into multiple lines.

data = '''Lorem Ipsum is simply dummy text. \nIt is one of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.'''
print(data)

Output

Lorem Ipsum is simply dummy text.
It is one of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.

In the output, you can see that the string starts with a newline with it encounter newline character within triple quotes of a long string.

Using brackets to define a multiline string

Another technique is to enclose the long string with rounded brackets. We can split the string into multiple lines using brackets.

data = ("LoremIpsum is simplydummytext. "
"It is one of the printing and typesetting industry. "
"Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.")
print(data)

Output

Lorem Ipsum is simply dummy text.
It is one of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.

You can see in the output that there is no newline character. However, if you need it, then add it while creating a new line string.

data = ("Lorem Ipsum is simply dummytext."
"\nIt is one of the printing and typesetting industry."
"\nLorem Ipsum has been the industry's standard dummy text ever since the 1500s.")
print(data)

Output

Lorem Ipsum is simply dummytext.
It is one of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.
Please note that the PEP 8 guide recommends using brackets to create Python multiline string.

Using backslash to create a multiline string

In Python, a backslash (\) is a continuation character, and if it is placed at the end of a line, it is considered that the line is continued, ignoring subsequent newlines.

sum = 6 + 2 \
    + 3

print(sum)

Output

11

You will not get any error because ( \ ) character is a continuation character, and the interpreter knows that very well. This also applies to strings. You can use the backslash and write the multiline strings.

One thing to note here that this approach is a less preferred way for line continuation. However, it absolutely works and can join strings on various lines.

data = "Lorem Ipsum is simply dummytext. \
It is one of the printing and typesetting industry. \
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s."
print(data)

Output

Lorem Ipsum is simply dummytext. It is one of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.

You can inspect that the output isn’t showing any newlines. However, you can do like to add some by yourself. Use the newline character (\n) to split the strings into the new line from whenever you want.

Using join() function to create a multiline string

The final approach is using the string join() function to convert the string into multiline. It manages the space characters itself while contaminating the strings.

data = '\n'.join(("Lorem Ipsum is simply dummytext.",
                  "It is one of the printing and typesetting industry.",
                  "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s."))
print(data)

Output

Lorem Ipsum is simply dummytext.
It is one of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.

In this example, we have joined three strings using join() function with a newline character. That is why you can see three strings on multiline in the output.

Wrapping up

Write a multiline string using brackets because that is the preferable way in the Python community. Again this all depends on the specific scenario you are facing, but a general rule of thumb is always to go with the community. We have seen the different ways to create and write multiline string so that we can see all the aspects of Python string.

See also

Python string split()

Python string rsplit()

Python string splitlines()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.