Python Program to Create a Long Multiline String

To understand this example, you should have the knowledge of the following Python programming topics:


Example 1: Using triple quotes

my_string = '''The only way to
learn to program is
by writing code.'''

print(my_string)

Output

The only way to
learn to program is
by writing code.

You can use '''(multiline string)''' or """(multiline string)""" to print a multiline string as shown above.


Example 2: Using parentheses and a single/double quotes

my_string = ("The only way to \n"
        	"learn to program is \n"
        	"by writing code.")

print(my_string)

Output

The only way to
learn to program is
by writing code.

If you use (" ") syntax, you need to specify the newlines explicitly using \n.


Example 3: Using \

my_string = "The only way to \n" \
        	"learn to program is \n" \
        	"by writing code."

print(my_string)

Output

The only way to
learn to program is
by writing code.

You can use \ as in the above example code to write a multiline string.

Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community