Python - How to Print String in Next Line Easily
Here is an idea of how to split long strings into multiple lines. There are two methods - Backslash and Triple quotes. Python made handling strings easier for users. Assigning strings to a variable is accepted. That variable you can call an Object. Python treats everything as an Object.
>>> s = "This is a test of the emergency broadcast system" \
… " and it will display an emergency if you put a line of
text" \
… " that is more than 80 characters on a single line"
>>> s1 = """ This is a test of the emergency broadcast
system.
… and it will display an emergency if you put a line of text
… that is more than 80 characters on a single line
… """
>>> print(s1)
This is a test of the emergency broadcast system.
and it will display an emergency if you put a line of text
that is more than 80 characters on a single line
Method1: Backslash Method
>>> s = "This is a test of the emergency broadcast system" \
… " and it will display an emergency if you put a line of
text" \
… " that is more than 80 characters on a single line"
>>> print(s)
This is a test of the emergency broadcast system and it will
display an emergency if you put a line of text that is more
than 80 characters on a single line
This is a test of the emergency broadcast system and it will
display an emergency if you put a line of text that is more
than 80 characters on a single line
Method2: Triple Quote Method
>>> s1 = """ This is a test of the emergency broadcast
system.
… and it will display an emergency if you put a line of text
… that is more than 80 characters on a single line
… """
>>> print(s1)
This is a test of the emergency broadcast system.
and it will display an emergency if you put a line of text
that is more than 80 characters on a single line
Summary
- You can use two methods to split the string
- The Tripple quote method is easier to use
Comments
Post a Comment
Thanks for your message. We will get back you.