Introduction to Python Multiline String
In Python, the string is a sequence of characters, and to declare string we use single quotes that are used for declaring a single-line string. In this multiline string in Python is defined using triple quotes such as “““ ”””. Therefore any string or any literals between these triple quotes is considered as a string. In the Python program, multiline strings are mainly used for specifying comments for describing the program working. In Python, the multiline string is the most recommended way to represent a string. In Python, when are using string more than one line and if they have a newline character then we use triple quotes, else there are different ways to represent a string that has no newline character.
Working of Python Multiline String with Examples
In this article, we will see how to declare or define a multiline string in Python. We should note that if we are using a lengthy string in the Python program which are mainly used for declaring comments in programs and this lengthy string with a newline character we can declare it using triple quotes ( “““ ””” ) or three single quotes( ‘ ‘ ‘ ). If we are trying to declare a lengthy string without a newline character then we cannot use triple quotes instead there are different ways for declaring such a string.
Firstly, we will see how to declare a multiline string in Python having a newline character using triple quotes along with examples.
Example:
Let us consider we want to declare a long string in the program. Then let us see how we can declare it using single and double triple quotes.
print("Program to demonstrate double and single triple quotes for multiline string")
print("\n")
double_qt = """Educba Training Institue,
provides different programming langauges,
in Chennai India."""
print(double_qt)
print("\n")
single_qt = '''Educba Training Institue,
provides different programming langauges,
in Chennai India.'''
print(single_qt)
Output:
In the above program, we can see how to declare a multiline string using double and single triple quotes. In the above program, we have written each and every line in the newline.
Now we will different ways to declare multiline string in Python. Let us see each of them along with examples. Therefore different ways to declare multiline string are as follows:
1. Using brackets
This is another way to declare a multiline string in Python when there is no newline in the string using brackets for declaring a multiline string. Let us see it in the example below:
Example:
print("Program to demonstrate to declaring multiline string using brackets")
print("\n")
brkt_str = ("EDUCBA is a leading global provider of skill based online education."
"EDUCBA is currently catering more than 500,000+ learners across 40+ countries.")
print(brkt_str)
Output:
In the above program, we can see we are using brackets to declare a multiline string. In this, we can note that in the output it will print the string declared without newline character as there is no newline character in the given string. Therefore we use brackets to split a string into multiple lines.
2. Using backslash ( \ )
In this option, we use a backslash ( \ ) for a multiline string in Python which does not contain a newline character in the given string. The option of using backslash is used for declaring string to print multiple lines. Let us demonstrate in the below example.
Example:
print("Program to demonstrate multiline string using backslash")
print("\n")
bck_str = "Educba Training Institue." \
"This institute is highly recommended for online programming courses and " \
"this course also provide certificates."
print("The multiline string is given as follows:")
print("\n")
print(bck_str)
Output:
In the above program, we can see in that each string at the end we have given backslash ( \ ) and the last string of the multiline string does not have backslash. Therefore we can see that given string does not have any newline character and therefore using backslash we can print the multiline string.
3. Using join()
In this option, we use string join() function of Python for splitting up multiple strings that are used for a printing multiline string. So in the above two options, there was a condition to check for spaces when we use either brackets or backslash because if we have very long string then it would be difficult if we miss out space when we use these two options but this problem can be overcome using string join() method. Let us demonstrate it below with an example.
Example:
print("Program to demonstrate multiline string using join()")
print("\n")
jn_str = ' '.join(("Educba Training Institue",
"This institute is highly recommended for online programming courses and",
"this course also provide certificates."))
print("The given string is as follows:")
print("\n")
print(jn_str)
Output:
In the above program, we can see we are using join() function of string. In this, we can notice that we are passing the multiline string to this function. Therefore it prints the multiple lines of string using the join() function and we can note that we are using space and then using the join() function to it. Therefore this is easy when we are using lengthy string and there is no need for space check in the string when we join() function of string in Python.
Conclusion
In this article, we conclude that a multiline string in Python is to print the multiple lines of string. In this article, we saw a multiline string that is mainly used in printing comments specified in the program. This can be done using triple quotes such as single and double triple quotes. In this article we saw when the string has newline characters then we have to use triple quotes and if there is no newline characters then we have discussed various ways for declaring multiline string such as using brackets, backslash and join() function of string in Python.
Recommended Articles
This is a guide to Python Multiline String. Here we also discuss the introduction and working of python multiline string along with a different example and its code implementation. You may also have a look at the following articles to learn more –