Python print multiline string

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.

mulitline string in triple quotes

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.

Читайте также:  Search in java example

# 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.

python mulitline string in 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.

mulitline string using backslash

# 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.

python mulitline string using join method

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 .

Источник

Python Multiline String

Python Multiline String

In Python, the string is a sequence of characters, and to declare a 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 to specify comments describing the program’s working. In Python, the multiline string is the most recommended way to represent a string. In Python, when we are using a string with 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 with no newline character.

Web development, programming languages, Software testing & others

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 is 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 of 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.

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)

Python Multiline String-1.1

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 find different ways to declare multiline strings in Python. Let us see each of them, along with examples. Therefore different ways to declare multiline strings 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:

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)

Python Multiline String-1.2

In the above program, we can see we use brackets to declare a multiline string. In this, we can note that the output 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 that 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.

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

In the above program, we can see that for each string at the end, we have given a backslash ( \ ), and the last string of the multiline string does not have a backslash. Therefore we can see that the given string does not have any newline character, and therefore using a backslash, we can print the multiline string.

3. Using join()

In this option, we use the string join() function of Python to split up multiple strings used for printing multiline strings. 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 a 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.

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

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 strings and there is no need for a space check in the string when we join() function of the string in Python.

Conclusion

In this article, we conclude that a multiline string in Python is to print multiple lines of string. In this article, we saw when the string has newline characters, and then we have to use triple quotes. If there are no newline characters, we have discussed various ways for declaring multiline string, such as using brackets, backslash, and join() functions of string in Python.

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 –

Источник

Оцените статью