Python Programming

Print function in python

In Programming, Python by Baqir Ali

In every programming language, we need to print the output of a code. the same case is with the python programming language. In order to print a message in the python language, we use the print() function.

Printing String in python

print("Hello World")

It simply prints the message ‘Hello World’ on the screen. No need to put a semicolon/terminator at the end of the line like in other languages.

print("☺ "*10)

Prints smilies 10 times, (Press alt + 1) to print smilies.

print("Hello! I have %d Apples"%20)

Print format in Python

we can also use format specifier for python language as we have used in c language.

print("Hello! I have %d Apples"%20)
print("Out of %d I have eaten %.2f of them and remaining %.2f"%(20,2.5,17.5))

Output:

Hello! I have 20 Apples
Out of 20 I have eaten 2.50 of them and remaining 17.50

We use %d for integers and %f for floating-point numbers but in the above example %.2f the 2 after point(.) means that we want only two decimals after a point.

Print with format method in Python

print("Hello My Name is {0} and I am {1} years old and my Profession is {profession}".format("Elia",28,profession="programmer"))

Output:

Hello My Name is Elia and I am 28 years old and my Profession is programmer

Combing format method with format specifier and positional number in Python

print("{name} got {1:.2f}% marks in {0:2d} subjects".format(5,88.5,name='Elia'))

Output:

Elia got 88.50% marks in 5 subjects

Printing value of variables in Python

a=10
b=20
c=a+b
print("the sum of %d and %d is %d"%(a,b,c))

Output:

the sum of 10 and 20 is 30

pi=3.1416
radius=2.5
print("the area of a circle is %.2f"%(2*pi*radius*radius))

Output:

the area of a circle is 39.27

Support us by sharing this post