print() Function in python

Introduction

If you're a newbie to any programming language, the first thing you've to learn is the Basics and the basics start by printing something using a simple program. In this section, you will learn how to print in python and everything about the print() function.

Printing something in python is as easy as writing. Its versatility is much higher than that of other programming languages. We will go through several examples to know this better.

Print a Simple Line

See how easy it is to print a line through the print() function.


print("This is the Print Function")

Output

This is the Print Function

Escape Sequence

In python, you can use an escape character or backslash('\') to avoid the illegal characters in a string. Let's understand it through an example.

Example 1: The Problem

It will arise a syntax error.


print('It's My first Python Program')

Output

 File "/home/suvankar/Desktop/sample_code.py", line 3
    print('It's My first Python Program')
              ^
SyntaxError: invalid syntax

Example 2: The Solution

The solution to the above problem is to put an escape character before that illegal character (see the yellow mark) in that string.


print('It\'s My first Python Program')

Output

It's My first Python Program

Example 3

Here is another example of printing unlawful strings. See, "hosts" is an illegal format in the string but, we can avoid it by putting the '\' character before.


# "hosts" is an illegal format in the string
# But avoid those double quotes by putting \ character
# before it.
print("Every Operating System has a \"hosts\" file.")

Output

Every Operating System has a "hosts" file.

Raw Strings in Python

In python, you can print a raw string by putting 'r' before it. It'll treat all the backslashes(\) as literal characters. So it will be helpful when you'll try to print a string that contains so many backslashes; for example, the location of a file or a directory in windows or other operating systems.


# Put 'r' before the quotation
print(r"C:\Windows\System32\drivers\etc\hosts")

Output

C:\Windows\System32\drivers\etc\hosts

Print New Lines in Python

While writing a program we need to print new lines many times. In Python, this is done using '\n'.


print("1. First Line\n2. Second Line\n3. Third Line")

Output

1. First Line
2. Second Line
3. Third Line

Concatenates two Strings in the print() function

Surprisingly you can concatenate two strings easily in python. Let's see through an example.


# Concatenate two Strings
print("Welcome to" + " PySeek")

Output

Welcome to PySeek

Arithmetic Operation in print() Function

You can perform arithmetic operations in the print() function. It can be performed directly involving the numbers in the operation or through the pre-declared variables. The example below will illustrate this better.


# Sum of two numbers
print("Result 1: ",5+2)
# Multiplication
print("Result 2: ",30*0.08)
# Division
print("Result 3: ",90-2)

Output

Result 1:  7
Result 2:  2.4
Result 3:  88

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post