This article will teach everything there is to Python multiline string. It’s convenient when you have a string that takes up multiple lines. It is good practice to limit the length as very long lines can affect the readability of your code. Hence, we use multiline strings.
Let’s check out the available implementations for Python multiline strings.
Using 3 Single Quotes
This is the most common method to allow a single string to extend multiple lines. All the characters within triple single quotes are considered one string.
myString = '''Welcome to
PythonPool'''
print(myString)
Output
Welcome to PythonPool
Note that it maintains the indentations in the string. You may need to use triple single quotes if the string contains double quotes.
myString = '''Welcome to
"PythonPool" '''
print(myString)
Output
Welcome to "PythonPool"
Using 3 Double Quotes
Triple Double Quotes have the same functionality as single quotes. They allow lengthy text to be presented in a neatly organized format.
myString = """Welcome to
PythonPool"""
print(myString)
Output
Welcome to PythonPool
You may need to use triple double quotes if the string itself contains single quotes.
myString = """Welcome to
'PythonPool' """
print(myString)
Output
Welcome to 'PythonPool'
Using Parentheses to Create a Python Multiline String
It is also possible to write multiple strings within braces and store them as one string variable like so:
myString = ("Welcome to PythonPool! "
"We have over 500 articles! "
"Go-to spot for Python Related Articles! ")
print(myString)
Output
Welcome to PythonPool! We have over 500 articles! Go-to spot for Python Related Articles!
This is the PEP 8 recommended procedure for creating multiline strings.
Using Backslashes to Create Multiple Lines
Although its the least preferred, it is a simple way to create a multiline string as a single string type.
myString = "Welcome to PythonPool! " \
"We have over 500 articles! " \
"Go-to spot for Python Related Articles! " \
print(myString)
Output
Welcome to PythonPool! We have over 500 articles! Go-to spot for Python Related Articles!
Python String .join() Method
This method returns a string by affixing string elements of a sequence data type. A string-type separator separates each element.
myText = ["Welcome", "to", "PythonPool",".","We", "have", "over", "500", "articles"]
print(' '.join(myText))
Output
Welcome to PythonPool . We have over 500 articles
In the above demonstration, we pass the .join() method on a List consisting of string types. We combine them into one string by separating them with a space character.
How to Concatenate Python Multiline String
There are various ways to concatenate multiline strings. The PEP 8 recommended procedure for working with multiline strings is to use parentheses. Refer to the following demonstration.
myString1 = "Welcome to"
myString2 = "PythonPool"
myString3 = "Source for free python material"
concatString = ("myString1 : " +
myString1 +
"\n myString2: " +
myString2 +
"\n myString3: " +
myString3 )
print(concatString)
Output
myString1 : Welcome to myString2: PythonPool myString3: Source for free python material
concatString
may consist of 3 different types of strings. However, they’re all considered as one single multiline string.
Creating Multiline Strings in JSON?
In most programming languages, the concept of multiline strings may not exist. However, developers overcome this issue by using newline characters and string concatenation. This is the same case in JSON as well.
One should note that JSON isn’t technically a programming language itself. It is a format to represent structured data based on JavaScript object syntax. It does not have any basic infrastructure to allow the existence of multiple line strings.
We know that multiline strings mainly improve human readability for long lines of code, But JSON was designed for better system readability.
How to Indent Multiline strings in Python
Indentation for multiline strings is mainly used for visual appeal for other people who review your code. Check out the below implementation.
myString = """first line
second line
third line"""
For Python Multiline strings, this is proper indentation. The second line of your string should align with the triple quotes.
Difference between Python Multiline String and a Comment line
Multiline String | Comment Line |
---|---|
A multiline string is considered a standard string type. | A comment line or a multiline comment line is a string literal and not a string type. |
You can perform all the operations you would on a normal string in multiline strings. | It does not represent any kind of operation, it just acts as a visual description in the source code. |
FAQs
inspect.cleandoc()
function allows you to improve the visual appearance of docstrings in your code. This function manages things like indentation and whitespace in order for it to line up with your blocks of code.
Conclusion
Multiline strings are a great tool to increase the readability of your source code. We have discussed various methods that allow you to create multiline strings.