Displaying Texts In Python

Introduction

In this article I will show you different ways to display texts in python. In python the print statement is used to display text.



In python with print statement you can use Single Quotes(') or Double Quotes(").  The output of the above program will be as in the following screenshot:

 

Here you can see our output is in multiple lines so by default each print statement prints output in new line.

Does it matter if you use single quotes or double quotes:

Well there is no matter to print in single quotes or double quotes but what happens when you print single quotes or double quotes.

Example: 

 

Here in the first print statement there is no error because that is in double quotes and I am printing single quotes in double quotes that is fine. 
But in second print statement which is printed in single quote and I am printing single quote inside single quotes which gives error.

"You cannot print single quotes inside single quotes".

Same like:

"You cannot print double quotes inside double quotes".

 

So If I had a single quote(s) inside the string then I would use double quotes in print statement and  if I had double quotes inside the string then I would use single quotes in print statement.

Question: What would be if my string had both single quotes and double quotes?

Answer:  There are two ways to print.
1st way: You can use both plus(+) sign to concat two strings in python. And with + sign you can print.

 
Output: 

 

2nd way: You can use \' to print single quote or \" to print double quote like any other programming language.

 

Output:

 
 
Display texts in Multiple Lines

As we have seen above that multiple print statements display texts in multiple lines. But there are two more ways to display texts in multiple lines.
  1. Use of \n: To print text in multiple lines you can use \n for next line of text like any other programming language.

    Example:
    1. print('Love your job\n but don\'t love your company,\n because you may not know when your company stops loving you.')  
    Output:



  2. Use of Triple Quotes: You can use triple quote single quotes or triple double quotes to print multiline statements.

    Example:



    or



 
Python Escape Sequence

Escape Sequence or Escape Character are used to signal an alternative interpretation of a series of characters. Most commonly, escape characters are used to solve the problem of using special characters inside a string declaration.

Python escape sequence lists are given below:

Escape CharacterDescriptionExampleOutput
\'Prints Single Quotesprint(‘It\’s’)It’s
\''Prints Double Quotesprint(“\”C-sharpcorner\””)”C-sharpcorner”
\\Prints Single backslashprint(‘\\n for newline’)\n for new line
\nNewlineprint(‘\newyork’)ewyork
\rCarriage Returnprint(‘1\r2’)2
\tHorizontal Tabprint(‘1\t2’)1 2
\bBackspaceprint(‘12\b3’)13
\uxxxx16 Bit Unicode"Katakana a: \u30A1"Katakana a: ã‚¡
\Uxxxxxxxx32 Bit Unicode"Katakana a: \u000030A1"Katakana a: ã‚¡

Up Next
    Ebook Download
    View all
    Learn
    View all
    sourabhsomani.com