Python

How to write a multiline string in Python

April 17, 2022

How to write a multiline string in Python

In this article you will learn how to write a multiline string in Python – using the language’s own syntax for multiline strings.

multiline string in Python thumb

Hey guys, what’s up? Let’s learn more about Python and its string forms.

Strings in Python are characterized by single or double quotes, in this way it is possible to create a string

But when we need to write strings with many lines in Python we need to use a special syntax

And this special syntax is called triple quotes, because it is precisely its representation form, see a practical example:

manyLines = """ A
text
wiht
múltiple
lines"""

print(manyLines)

Multiline strings can also be written by triple single quotes, not the difference in the final result

Another strategy would be to use the \n syntax which is a line break in strings

But triple quotes end up being simpler, don’t you agree?

This is not used often, but it’s a good thing to have in hand, if we need to use it in a future project that we will work

Conclusion

In this article we learned  how to write a multiline string in Python

We just need to add the text in triple quotes, whether they are single or double, we will create a multi-line string in Python

Remembering that there is also the possibility of breaking the line with \n

Now just choose the variation that best fits your project!

Do you want to learn more about Python and web development? Click here!

You can also see our blog categories, that can improve your knowledge in many other technologies and languages 🙂

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x