Python Line Continuing a Large String

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 string join() function in Python which is used to produce a string containing newlines.

Python String

Create a Python Multiline String with Examples

Let's now discuss each of these options in details. We have also provided examples with the description of every method.

Use triple quotes to create a multiline string

mulitline string in triple quotes

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 second in the end.

"""Learn Python Programming"""

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

python mulitline string in brackets

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.

Backslash to join string on multiple lines

mulitline string using backslash

It is a less preferred way to use 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)

The output:

I'm learning Python. I refer to TechBeamers.com tutorials. It is the most popular site for Python programmers.

Join() method to create a string with newlines

python mulitline string using join method

The final approach is applying the string join() function to convert a string into multiline. It handles the space characters itself while contaminating the strings.

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

The result is:

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 in using Python multiline string. 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 .

westmorly2002.blogspot.com

Source: https://www.techbeamers.com/python-multiline-string/

0 Response to "Python Line Continuing a Large String"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel