In Python, the print() function is used to display text on the screen. It can be used to print text, variables, and the results of mathematical operations.
Here are a few examples of how to use the print() function:
print("Hello, World!") # Prints the string "Hello, World!" x = 5 y = 10 print(x + y) # Prints the result of x + y (15) name = "Alice" print("My name is", name) # Prints "My name is Alice"
Output:
Hello, World! 15 My name is Alice
You can also use print() function to print multiple values by using a comma to separate them.
x = 5 y = 10 print(x, y) # Prints 5 10
In python 3, print() is a function and it accepts multiple arguments and you can use the sep parameter to specify the separator between the arguments.
x = 5 y = 10 print(x, y, sep=',') # Prints 5,10
Output:
5,10
You can also use the end parameter to specify the character that should be printed after the last argument. By default, it is a newline character.
x = 5 y = 10 print(x, y, end='. ') print(x*y) # Prints 5,10. 50
Output:
5,10. 50
print() is a very useful function in Python and you will use it frequently while programming.