How to Create a Multiline String in Python



Python


In this article, we show how to create a multiline string in Python.

So a multiline string is a string that spans multiple lines.

Normally when you're using a function such as the print() function, you pass into it a one-line string, such as, print('hello world'). However, this doesn't have to be the case. You may want to pass in a string that spans multiple lines (a multiline string).

In order to create a multiline string in Python, we use triple quotes (''') to enclose the string. So we use a triple quote (''') as the opening string and a triple quote (''') as the closing string.

In the following code below, we show an example of a multiline string in Python.



So we've created a print function that has a multiline string in it. We create it by enclosing the string in triple quotation marks.

With a multiline string passed into the print() function, each line prints on its own line.

We then show this again but now we create a multiline string and pass it into the variable, string1.

We then pass this variable, string1, into the print() function.

And this is how to create a multiline string in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...