Python summary: printing

The print() function is used to displaying data on the screen (via reference, math result…). Some examples:

>>> print("Hello, world") # A text
Hello, world
>>> price = 9
>>> print(price) # A number
9
>>> friendname = "Esteban"
>>> print("You are", friendname) # A text and passed variable value
You are Esteban

It is even possible do maths on a print() function:

>>> print(2 + 2)
4
>>> print("Hello!" * 3) # Multiplying a string
Hello!Hello!Hello!

 

Moreover, print() creates a default spacing in the next cases:

(1) Between words, after the first one:

>>> print("Apples","bananas","pears")
Apples bananas pears

We can change or delete it by employing the word sep=””:

>>> print("Apples","bananas","pears",sep=", ")
Apples, bananas, pears

(2) When there is another print() then, at the end:

>>> print("Apples"); print("bananas"); print("pears")
Apples
bananas
pears

I have wrote semicolons (;) because I put several statements in the same line, and they are needed to delimit them ones.

Well, if we want to change such second spacing, we must use end=””:

>>> print("Apples",end=""); print("bananas",end=""); print("pears",end="")
Applesbananaspears

(1+2) And to use both ones also are possible:

>>> print("Apples", "bananas" ,"pears" , sep=", ", end="...")
Apples, bananas, pears...
Imprimir

Be First to Comment

Deja un comentario