Everything about print() in python

Everything about print() in python

Everything about print() in python

Did you liked it ??
+1
0
+1
0
+1
0
+1
0

print() function:

Python print() function is used to print something on the screen. For printing we need to use print() function. Strings are the collection of character inside “double quotes” or ‘single quotes’.

If we observe then print is not a statement it is a function. It is an in-built python function.

sep: It is a key word that is used to seperate string and insert some values or some default space. Let’s see some examples

Rather than using \n or \t, we can also use symbols like comma (,) or plus (+) sign.

To display a variable’s value along with a predefined string, all you need to do is add a comma in between the two. Here the position of the predefined string and the variable does not matter.

Similar to a format argument where your print function acts as a template, you have a percentage (%) sign that you can use to print the values of the variables.

Like format argument, this also has a concept of placeholders. However, unlike the format function where you pass in just the index numbers, in this, you also need to specify the datatype the placeholder should expect.

%d is used as a placeholder for numeric or decimal values. %s is used as a placeholder for strings.

Formatting:

A good way to format objects into your string for print statement is with the string. Here two method are used.

1)Format Method

Syntax:

‘String here { } then also here { }’. format(‘something1’,’something2)

2)f-string (formatted string literals)

 

Also read:

CODE FOR PRACTICE:

print("Hello World")

print('Hello World')

#type() of print
type(print)

print('Python','tutorial','of','data crux')

print('Python','tutorial','of','data crux',sep='\n') #\n will put each word in a new line

print('Python','tutorial','of','data crux',sep=',')

print('Python','tutorial','of','data crux',sep='\n\n')

print('Python','tutorial','of','data crux',sep='+')

a = 3
b = "Datacux"
print(a,"is an integer while",b,"is a string.")

print("{0} is an integer while {1} is a string.".format(a,b))

print("%d is an integer while %s is a string."%(a,b))

print(f'{a} is an integer while {b} is a string')

TEST YOUR KNOWLEDGE !

0%

What is %s used for?

Correct! Wrong!

%s is always used to represent string. If you use integer with %s then it will perform typecasting.

How many are there in formatting?

Correct! Wrong!

There are two methods of formatting using format() method and formatting string.

Can we use other symbol in seperator sep() as well?

Correct! Wrong!

Yes!!!

print() function quiz
Did you liked it ??
+1
0
+1
0
+1
0
+1
0

Leave a Reply

Your email address will not be published. Required fields are marked *