- Multiline String in Python with Examples
- Create a Python Multiline String with Examples
- Use brackets to define a multiline string
- Python Join() method to create a string with newlines
- 4 Techniques to Create Python Multiline Strings
- Triple quotes to create multiline strings in Python
- Using backslash (\) for multiline string creation
- The string.join() method to build a Python multiline string
- Python round brackets () to create multiline strings
- Summary
- Python Multiline Strings
- Example
- Example
- Related Pages
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
Multiline String in Python with Examples
This tutorial explains how to create a Python multiline string. It can be handy when you have a very long string. You shouldn’t keep such text in a single line. It kills the readability of your code.
In Python, you have different ways to specify a multiline string. You can have a string split across multiple lines by enclosing it in triple quotes. Alternatively, brackets can also be used to spread a string into different lines.
Moreover, backslash works as a line continuation character in Python. You can use it to join text on separate lines and create a multiline string. Finally, there is the string join() function in Python which is used to produce a string containing newlines.
Create a Python Multiline String with Examples
Let’s now discuss each of these options in detail. We have also provided examples with descriptions of every method.
It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and the second at the end.
Anything inside the enclosing Triple quotes will become part of one multiline string. Let’s have an example to illustrate this behavior.
# String containing newline characters line_str = "I'm learning Python.\nI refer to TechBeamers.com tutorials.\nIt is the most popular site for Python programmers."
Now, we’ll try to slice it into multiple lines using triple quotes.
# String containing newline characters line_str = «I’m learning Python.\nI refer to TechBeamers.com tutorials.\nIt is the most popular site for Python programmers.» print(«Long string with newlines: \n» + line_str) # Creating a multiline string multiline_str = «»»I’m learning Python. I refer to TechBeamers.com tutorials. It is the most popular site for Python programmers.»»» print(«Multiline string: \n» + multiline_str)
After running the above, the output is:
Long string with newlines: I'm learning Python. I refer to TechBeamers.com tutorials. It is the most popular site for Python programmers. Multiline string: I'm learning Python. I refer to TechBeamers.com tutorials. It is the most popular site for Python programmers.
This method retains the newline ‘\n’ in the generated string. If you want to remove the ‘\n’, then use the strip()/replace() function.
Use brackets to define a multiline string
Another technique is to enclose the slices of a string over multiple lines using brackets.
See the below example to know how to use it:
# Python multiline string example using brackets multiline_str = ("I'm learning Python. " "I refer to TechBeamers.com tutorials. " "It is the most popular site for Python programmers.") print(multiline_str)
It provides the following result:
I'm learning Python. I refer to TechBeamers.com tutorials. It is the most popular site for Python programmers.
You can see there is no newline character in the output. However, if you want it, then add it while creating the string.
# Python multiline string with newlines example using brackets multiline_str = («I’m learning Python.\n» «I refer to TechBeamers.com tutorials.\n» «It is the most popular site for Python programmers.») print(multiline_str)
Here is the output after execution:
I'm learning Python. I refer to TechBeamers.com tutorials. It is the most popular site for Python programmers.
Please note that PEP 8 guide recommends using brackets to create Python multiline string.
It is a less preferred way to use a backslash for line continuation. However, it certainly works and can join strings on various lines.
# Python multiline string example using backslash multiline_str = "I'm learning Python. " \ "I refer to TechBeamers.com tutorials. " \ "It is the most popular site for Python programmers." print(multiline_str)
The above code gives the following result:
I'm learning Python. I refer to TechBeamers.com tutorials. It is the most popular site for Python programmers.
You can observe that the output isn’t showing any newlines. However, you may like to add some by yourself.
# Python multiline string example using backslash and newlines multiline_str = "I'm learning Python.\n" \ "I refer to TechBeamers.com tutorials.\n" \ "It is the most popular site for Python programmers." print(multiline_str)
I'm learning Python. I refer to TechBeamers.com tutorials. It is the most popular site for Python programmers.
Python Join() method to create a string with newlines
The final approach is applying the string join() function to convert a string into a multiline string. It handles the space characters by itself while contaminating the strings.
See the below sample code using the Python join().
# Python multiline string example using string join() multiline_str = ' '.join(("I'm learning Python.", "I refer to TechBeamers.com tutorials.", "It is the most popular site for Python programmers.")) print(multiline_str)
It outputs the following result:
I'm learning Python. I refer to TechBeamers.com tutorials. It is the most popular site for Python programmers.
# Python multiline string with newlines example using string join() multiline_str = ''.join(("I'm learning Python.\n", "I refer to TechBeamers.com tutorials.\n", "It is the most popular site for Python programmers.")) print(multiline_str)
I'm learning Python. I refer to TechBeamers.com tutorials. It is the most popular site for Python programmers.
We hope that after wrapping up this tutorial, you should feel comfortable using Python multiline strings. However, you may practice more with examples to gain confidence.
Also, to learn Python from scratch to depth, do read our step-by-step Python tutorial .
4 Techniques to Create Python Multiline Strings
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.
Triple quotes to create multiline strings in Python
The triple quotes can be used to display multiline strings in Python.
- 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.
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)
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.
variable = "string1"\"string2"\"stringN"
- While creating multiline strings using a backslash(\), the user needs to explicitly mention the spaces between the strings.
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)
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.
string.join(("string1","string2","stringN"))
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)
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.
variable = ("string1""string2""stringN")
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)
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.
Python Multiline Strings
You can assign a multiline string to a variable by using three quotes:
Example
You can use three double quotes:
a = «»»Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.»»»
print(a)
Example
a = »’Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.»’
print(a)
Note: in the result, the line breaks are inserted at the same position as in the code.
Related Pages
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.