You can use print() Function to print or echo data to the console .
# How to Print to Console in Python
print("Hello World! Welcome to Python Examples.")
print(10)
Execution and output :
You can pass in a string , Or a number , Or any other data type .
At the end of the print, the line feed will be added automatically .
In this example, we simply print a string to the console output . Pass the string as an argument to print() function .
# Print String to Console
print("Hello World!")
Execution and output :
Pass numbers as parameters to print() function .
# Print number to console
print(526)
Execution and output :
We can provide multiple variables as parameters to print() function . They will print to the console by default with a single space as a separator .
# Print Variable to Console
x = "pi is"
y = 3.14
print(x, y)
Execution and output :
You can pass a specific separator to sep Parameters .
# Python Print with a Specific Separator
x = "pi is"
y = 3.14
print(x, y, sep=" : ")
Execution and output :
stay Python in ,print() By default, the function uses the newline character as the end character . We can pass a specific terminator to end Parameter to override it .
# Python Print with a Specific End
print("Hello", end="-")
print("World", end=".\n")
Execution and output :
You can use input() The function reads a number entered by the user in the console . actually Python3 It doesn't tell whether your input is a string or a number . Whatever you type in , It's all treated as a string .
To treat input as a number , It needs to be cast to an integer type .
# Read Number from Console
n = int(input("Type a number: "))
print(n)
Execution and output :
You can use some mathematical operations to see if it's a number .
# Check if it is a number by performing some arithmetic operations
n = int(input("Type a number: "))
print(n)
print(n + 7)
Execution and output :
You can use input() Function reads a string from the console as input to your program .
# Read String from Console
inputStr = input("Enter a string: ")
print(inputStr)
Execution and output :
input() Function will return the string you entered in the console .
print() By default, the function uses a newline character as the end of the print , To be in Python If there is no line break between multiple printings , You can be right print() Functional end=’’ Parameter setting .
This example shows how to execute more than once print() Do not wrap .
# Print without new line
print("Hello", end='')
print(" World.", end='')
print(" Welcome to", end='')
print(" defonds.net")
Execution and output :