How Can I Define Multiline String In Python

Gabriel9999

Member
Joined
Mar 12, 2019
Messages
38
Reaction score
4
Credits
130
How can I define multiline string Python and print to the screen? Thanks in advance for your help!
 


JasKinasis

Well-Known Member
Joined
Apr 25, 2017
Messages
1,321
Reaction score
1,870
Credits
8,343
You don't need to define a multiline string. It's just a string!

The simplest way to do it is to enclose the entire string with three sets of double quotes:
e.g.
Code:
message = """This is a long string
which contains multiple
lines of text"""

print(message)
The above method is probably the most pythonic way of doing this. Everything between each set of triple quotes (including line-breaks) is put inside the string.

Another way:
Code:
message = ("This is a long string\n"
        "which contains multiple\n"
        "lines of text")
print(message)
In the above method, we have to use escape characters to add the line-breaks.

You could also do it all in one line:
Code:
message = "This is a long string\nwhich contains multiple lines\nof text"

print(message)
Again, this method requires you to manually put in the line-breaks.

So there are three ways of creating a multi-line string where you know what the string will contain from the outset.

But if you have something like a list of strings that you want to join together in a multi-line string - Perhaps they were read from a file, or they were lines of text entered by a user of your program - you could do something like this:
Code:
lines = ["line 1", "line 2", "line 3"]

# Join the lines and print our message:
message = "\n".join(lines)
print(message)
In the above code, where we create the "message" variable, we are basically saying join everything in the "lines" variable (our list of lines), using a '\n' (newline character) between each item and store it in a variable called "message".
You'll have to imagine that the lines we declared above were the run-time contents of the lines variable at the time that we want to join the lines. When doing things like this in a real program, you might want to check that the list of lines is not empty before attempting to join them together. But that is for you to decide in your program. The most important thing in the above snippet is String's "join" method.

The above snippet would be handy if you were going to use the message string elsewhere in your program. But if you are only going to print the string once, the above code could be optimised to:
Code:
lines=["line 1", "line 2", "line 3"]
print( "\n".join(lines))

And if the strings in the list of strings already contain newline characters, you would do this:
Code:
lines=["line 1\n", "line 2\n", "line 3\n"]
print("".join(lines))
The above will join the lines using an empty string as a separator between each item in the list.

So there are multiple ways of creating string objects that contain multiple lines of text.
I hope this helps.
 
Last edited:

nandanandan

New Member
Joined
Apr 29, 2019
Messages
1
Reaction score
1
Credits
0
Code:

data = ''' Thi is a new
multiline string
with 3 different lines written inside'''

print(data)

Output:

Thi is a new
multiline string
with 3 different lines written inside
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Members online


Latest posts

Top