4 Techniques to Create Python Multiline Strings

Python Multiline Strings Thumbnail

A Python multiline string is the most efficient way of presenting multiple string statements in a formatted and optimized manner.

During the program, there may be a need to store long strings, which cannot be done in one line, it makes the code look horrible and not preferable, so the best way to handle this problem is multiline strings.

In this tutorial, we will be focusing on the different techniques that can be used to create Python multiline strings.

Also Read: Strings in Python

Triple quotes to create multiline strings in Python

The triple quotes can be used to display multiline strings in Python.

Syntax:

variable = """ strings """
  • If the input contains string statements with too many characters, then triple quotes can serve us with the need to display it in a formatted way.
  • Everything that comes under the triple quotes is considered as the string itself.

Example:

inp_str = """You can find the entire set of tutorials for Python and R on JournalDev.
Adding to it, AskPython has got a very detailed version of Python understanding through its easy to understand articles."""
print(inp_str)

Output:

You can find the entire set of tutorials for Python and R on JournalDev.
Adding to it, AskPython has got a very detailed version of Python understanding through its easy to understand articles.

The above output explains the advantage of the triple quotes as it printed the string in the same order as it is written in the code.

Using backslash (\) for multiline string creation

The escape sequence — backslash ('\') is used to create multiline strings in Python.

Syntax:

variable = "string1"\"string2"\"stringN"
  • While creating multiline strings using a backslash(\), the user needs to explicitly mention the spaces between the strings.

Example:

inp_str = "You can find the entire set of tutorials for Python and R on JournalDev."\
"Adding to it, AskPython has got a very detailed version of Python understanding through its easy to understand articles."\
"Welcome to AskPython!!"
print(inp_str)

Output:

You can find the entire set of tutorials for Python and R on JournalDev.Adding to it, AskPython has got a very detailed version of Python understanding through its easy to understand articles.Welcome to AskPython!!

In the above output, we can see that it does not handle spaces between strings. The user has to mention it at the time of declaration of multiline strings.

The string.join() method to build a Python multiline string

Python string.join() method has turned out to be an efficient technique for creating Python multiline strings.

The string.join() method handles and manipulates all the spaces between the strings and the user does not need to worry about the same.

Syntax:

string.join(("string1","string2","stringN"))

Example:

inp_str =' '.join(("You can find the entire set of tutorials for Python and R on JournalDev.",
                   "Adding to it", 
                   "AskPython has got a very detailed version",
                   "of Python understanding through its easy to understand articles.",
                   "Welcome to AskPython!!"))
print(inp_str)

Output:

You can find the entire set of tutorials for Python and R on JournalDev. Adding to it AskPython has got a very detailed version of Python understanding through its easy to understand articles. Welcome to AskPython!!

Here we can see that the string contains space even though we have not specified it, this is why string.join() method is highly recommended to create Python multiline strings.

Python round brackets () to create multiline strings

Python brackets can be used to create multiline strings and concatenate strings.

The only drawback of this technique is that the user needs to explicitly mention the spaces between the multiline strings.

Syntax:

variable = ("string1""string2""stringN")

Example:

inp_str =("You can find the entire set of tutorials for Python and R on JournalDev."
                   "Adding to it "
                   "AskPython has got a very detailed version "
                   "of Python understanding through its easy to understand articles."
                   "Welcome to AskPython!!")
print(inp_str)

Output:

You can find the entire set of tutorials for Python and R on JournalDev.Adding to it AskPython has got a very detailed version of Python understanding through its easy to understand articles.Welcome to AskPython!!

In the above output, we can see that spaces are not present between each string, which is a drawback of this method, so it is not recommended.

Summary

  • Python multiline strings are strings split into multiple lines to enhance the readability of the code for the users.
  • Python brackets, backslash, and triple quotes can be used to create multiline strings but here, the user needs to mention the use of spaces between the strings.
  • Python string.join() method is considered a very efficient way to create multiline strings and moreover, the spaces between the strings are implicitly handled by the method.
  • Python indentation rules are not applicable to multiline strings.
  • All the escape sequences such as newline(\n), and tab-space(\t) are considered as a part of the string if the multiline string is created using triple quotes.

Reference

https://stackoverflow.com/questions/10660435/how-do-i-split-the-definition-of-a-long-string-over-multiple-lines