Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

4 Techniques to Create Python Multiline Strings

Python Multiline String

Python has got different ways to represent strings. A Python multiline string is the most efficient way of presenting multiple string statements in a formatted and optimized manner.

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

Technique 1: Triple quotes to create multiline strings in Python

The triple quotes can be used to display multiple strings together i.e. multiline strings in Python.

Syntax:

variable = """ strings """
  • If our 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.

Technique 2: 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 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)

As clearly seen below, it does not manage the spaces between the statements. The user has to mention it at the time of declaration of the multiline strings.

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!!

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

Python string.join() method has turned out to be an efficient technique to create 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!!

Technique 4: Python round brackets () to create multiline strings

Python brackets can be used to create multiline strings and split the strings together.

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!!

Summary

  • Python multiline strings are the 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 to be a very efficient way to build 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), tab-space(\t) are considered as a part of string if the multiline string is created using triple quotes.

Conclusion

Thus, in this article, we have understood the various ways of creating Python multiline strings.


References