w3resource

Python: Print here document

Python Basic: Exercise-13 with Solution

Multi-line Here Document

Write a Python program to print the following 'here document'.

Sample string:
a string that you "don't" have to escape
This
is a ....... multi-line
heredoc string --------> example

Sample Solution:

Python Code:

# Use triple double-quotes to create a multi-line string
print("""
a string that you "don't" have to escape
This
is a  ....... multi-line
heredoc string --------> example
""")

Sample Output:

a string that you "don't" have to escape                                                                      
This                                                                                                          
is a  ....... multi-line                                                                                      
heredoc string --------> example 

Explanation:

A here document (or "heredoc") is a way of specifying a text block, preserving the line breaks, indents, and other whitespace within the text. The said code uses triple quotes to define a multi-line string, also known as a heredoc string. It allows the string to span multiple lines without the need to escape newline characters.

The output will be the string exactly as it appears in the code, including the newline characters.

Flowchart:

Flowchart: Print a specified document .

Python Code Editor:

 

Previous: Write a Python program to print the calendar of a given month and year.
Next: Write a Python program to calculate number of days between two dates.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/python-basic-exercise-13.php