Python multiline string
In this post, you will learn how to deal with multi-line strings using the Python programming language. There are lots of advantages of using multi-line strings instead of a single string if you have a multi-line paragraph to display. This enhances the readability of users. Keeping all the content in a solitary line makes it hard to read and looks awkward. It gives clearer meaning on the off chance that you compose it into numerous lines.
In Python, you have various approaches to indicating a multi-line string. You can have a string part across numerous lines by enclosing it in triple statements.
Python multi-line string using triple quotes
We can use triple double quotes to write multi-line strings. All that goes under the triple quotes is considered the string itself. If our information contains string statements with such a large number of characters, at that point, triple quotes can serve us with the need to show it in an arranged manner. The following example demonstrates this-
str1 = """Welcome to etutorialspoint.
It provides good educational resources to
technical students, learners, web developers. """
print(str1)
Output of the above code-
Welcome to etutorialspoint.
It provides good educational resources to
technical students, learners, web developers.
We can also use three single quotes to write multi-line strings.
str1 = '''Welcome to etutorialspoint.
It provides good educational resources to
technical students, learners, web developers. '''
print(str1)
Output of the above code-
Welcome to etutorialspoint.
It provides good educational resources to
technical students, learners, web developers.
Python multi-line string using brackets
We can also use brackets to split a string into multiple line strings.
str1 = ("Welcome to etutorialspoint. "
"It provides good educational resources to "
"technical students, learners, web developers. ")
print(str1)
Output of the above code- Python multiline string using backslash
In Python, the backslash function acts as a line continuation character. Using backslashes, we can split a string into multiple lines in Python. We use it to join text of independent lines. While making multi-line strings utilising backslash(\), the client needs to explicitly make reference to the spaces between the strings.
str1 = "Welcome to etutorialspoint. "\
"It provides good educational resources to "\
"technical students, learners, web developers. "
print(str1)
Output of the above code - Python multiline string using join()
In Python, we can use the string join() function to split a line into multiple lines of a string. It handles the space characters itself while sullying the strings. The following example demonstrates this-
str1 = ' '.join(("Welcome to etutorialspoint. ",
"It provides good educational resources to ",
"technical students, learners, web developers. "))
print(str1)
Output of the above code - Related Articles
Python multiline string
Alphabet pattern programs in Python
Python remove punctuation from string
Remove character from string Python
Count vowels in a string python
Count occurrences of each character in string Python
Convert string to list Python
Python split multiple delimiters
Python program to reverse a string
Permutation of string in Python
Remove spaces from string Python
splitlines in python
Python program to sort words in alphabetical order
Python split string by comma
Remove character from string Python
Check if two strings are anagrams Python
Printing Simple Diamond Pattern in Python
Stemming and Lemmatization in Python
Python | Generate QR Code using pyqrcode module