6. The print()
function¶
-
print
(*objects, sep=' ', end='\n')¶ The print() function is used to print text, numbers, and other objects to the screen.
It is a very useful tool when it comes to finding programming errors and to report on the development of the program.
The sep parameter indicates which character to use to separate the various objects that will be printed on the screen. Default is white space.
The end parameter indicates which character to use at the end of printing. It defaults to a new line '\n', but it can be replaced with an empty string '' so that successive
print
prints on the same line.Examples:
>>> print('Hola, mundo') Hola, mundo >>> print(3 + 5.6) 8.6 >>> print(1, 2, 3, 4, sep=' + ') 1 + 2 + 3 + 4 >>> print('Hola,'); print('mundo') Hola, mundo >>> print('Hola,', end=' '); print('mundo') Hola, mundo
Exercises¶
Write a program that prints multiple numbers separated by commas using the
sep
argument.Write a program that prints two words together on a single line. The first and second words must be printed with two separate
print()
commands.Write a program that calculates and prints the mean of three grades: 5, 8, 9
Pista: deberás sumar todas las notas dentro de un paréntesis y después dividir el resultado entre 3.
Write a program that prints 'Hello world' on two separate lines with a single
print()
command.Hint: the '\n' character is used to add a new line to a text string.
Write a program that assigns one value to one variables and then print the value of the variable on the screen.
Clue:
a = 5 print( ... )
Exit:
La variable 'a' tiene el valor 5
Write a program that assigns two values to two variables and then prints their values and the value of the sum.
Clue:
a = 9 b = 7 print( ... )
Exit:
La suma de 9 y de 7 es igual a 16