Multiline Strings in Python – POFTUT

Multiline Strings in Python


Python supports different types and format strings. In this tutorial, we will simply look at multi-line strings. We have the following code and want to type it in a single expression in multiline. As a practical programming language Python provides different ways to create multi-line strings.

Joining Line By Line

The most basic and simplest form is adding string lines step by step like below. We will use . in order to add strings together which will create a multi-line string which is stored in a variable named $html in this example.

html="<html><body>";
html.="</body></html>
echo $html

Double Quote

We can use a double quote by wrapping multiple lines. This will legal for python programming. And we can end the string whenever we want like below.

html="<html><body>
</body></html>"

3 Single Quote

By default, a single quote is used to express single-line strings. But 3 single quotes can be used to express multiline strings in Python programming language.

html='''<html><body><br>
</body></html>'''
3 Single Quote
3 Single Quote

Single Line

Basic for is a single-line string. We can use single line Python strings like below where a double quote with a single line stored in a variable that is HTML in this example. We will use \n  which means a new line.

html="<html><body>\n<br></body></html>"

EOF

EOF is a special keyword used to specify multi-line strings. The start of the multi-line string is marked with <<< EOF and then the string will be given. We also need to specify the end of the string with EOF too like below.

html= <<< EOF
<html><body>
</body></html>
EOF

Multiline Strings in Python Infographic

Multiline Strings in Python Infographic
Multiline Strings in Python Infographic

LEARN MORE  Linux Bash Shell Printf Command Examples

Leave a Comment