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.

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

Split Strings


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

  1. You can use two methods to split the string
  2. The Tripple quote method is easier to use

Comments

Most Popular

7 AWS Interview Questions asked in Infosys, TCS

Hyperledger Fabric: 20 Real Interview Questions

How to Fix Python Syntax Errors Quickly