Signup/Sign In

How to Create a long multi-line String in Python

In this article, we will learn to create a long multi-line string in Python. We will use some built-in functions, simple approaches available in Python for multi-line strings, and some related custom examples as well. Let's first have a quick look over what are multi-line strings in Python.

Python MultiLines Strings

The multiline strings are strings that are written in multiple lines. A long sentence written in a single line may not be readable and might look clumsy. So, Python introduces Multi lines Strings to express your single-line string in a more readable manner. Python indentation rules are not applicable to multiline strings. Multiline strings are formed using a long string enclosed within three double quotes, three single quotes, backslash, brackets, etc. While creating the multiline strings escape characters such as '\n', '\b', etc. become part of the string.

Different Ways to Create Multi-Line Strings in Python

There are various techniques and approaches to create multi-line strings in Python. We will create multiline strings using:

  1. three double quotes
  2. three single quotes
  3. backslash
  4. brackets
  5. join() function

Example: Create Multiline String Using Three Double Quotes

This method uses triple double quotes to create multiline strings. The string begins and ends with three double-quotes. While creating the multiline strings, escape characters (if present) such as '\n', '\t', etc. become part of the string.

string = """Oh, I get the shivers
I don't want to see a ghost,
It's a sight that I fear most
I'd rather have a piece of toast
And watch the evening news"""

print(string)


Oh, I get the shivers
I don't want to see a ghost,
It's a sight that I fear most
I'd rather have a piece of toast
And watch the evening news

Example: Create Multiline String Using Three Single Quotes

This method uses triple single quotes to create multiline strings. The string begins and ends with three single-quotes. While creating the multiline strings escape characters (if present) such as '\n', '\b', etc. become part of the string.

string = '''Oh, I get the shivers
I don't want to see a ghost,
It's a sight that I fear most
I'd rather have a piece of toast
And watch the evening news'''

print(string)


Oh, I get the shivers
I don't want to see a ghost,
It's a sight that I fear most
I'd rather have a piece of toast
And watch the evening news

Example: Create Multiline String Using parentheses

This method uses brackets to create multiline strings. These brackets are mainly round brackets or parentheses (). Brackets split the string into multiple lines. Each sentence of input string must be enclosed with double quotes and the string should begin and ends with round brackets. If you do not use '\n' at the end of each line, it will print the whole string into one line.

string = ("Oh, I get the shivers\n"
"I don't want to see a ghost,\n"
"It's a sight that I fear most\n"
"I'd rather have a piece of toast\n"
"And watch the evening news")

print(string)


Oh, I get the shivers
I don't want to see a ghost,
It's a sight that I fear most
I'd rather have a piece of toast
And watch the evening news

Example: Create Multiline String Using a backslash

This method uses backslash '\' to create multiline strings. The backslash is present at the end of each line. In Python, backslash works as a line continuation character. We use it to join text which is separate lines. Each sentence of the input string must be enclosed with double-quotes. If you do not use '\n' at the end of each line, it will print the whole string into one line.

string = "Oh, I get the shivers\n"\
"I don't want to see a ghost,\n"\
"It's a sight that I fear most\n"\
"I'd rather have a piece of toast\n"\
"And watch the evening news"

print(string)


Oh, I get the shivers
I don't want to see a ghost,
It's a sight that I fear most
I'd rather have a piece of toast
And watch the evening news

Example: Create Multiline String Using using join() Function

This method uses join() to create multiline strings. Using join(), we can split a string into multiple lines. The advantage of using the join() function over brackets or backslash way is that there is no need to worry about the spaces or double spaces. Each sentence of the input string must be enclosed with double-quotes. If you do not use '\n' at the end of each line, it will print the whole string into one line.

string = ''.join(("Oh, I get the shivers\n"
"I don't want to see a ghost,\n"
"It's a sight that I fear most\n"
"I'd rather have a piece of toast\n"
"And watch the evening news"))

print(string)


Oh, I get the shivers
I don't want to see a ghost,
It's a sight that I fear most
I'd rather have a piece of toast
And watch the evening news

Conclusion

In this article, we learned to create a multi-line string using triple-double and single quotes, backslash, brackets, and string.join() function. We saw different examples using each approach. You can also try using different long strings, escape sequences between the words of the strings and observe the output.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.