Python Program to Create a Long Multiline String

Here are four ways to create a long multi-line string in Python:

  1. Using “triple quotes”
  2. Using “brackets”
  3. Using “backslash”
  4. Using a “join()”

Method 1: 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 if you have split the lines in that long string.

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 to note is that anything that goes inside triple quotes is a string value, so if your long string has many newline characters, 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.

Method 2: 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.

Method 3: 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 the ( \ ) character is a continuation character, and the interpreter knows that very well. This also applies to strings. So you can use the backslash and write the multiline strings.

One thing to note is that this approach is less preferred for line continuation. However, it 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.

Method 4: Using the join() function to create a multiline string

The final approach uses the string join() function to convert the string into a multiline. It manages the space characters themselves 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.

That’s it.

See also

Python string rsplit()

Python string splitlines()

Leave a Comment

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