Input and Output in Python

Input and output are essential concepts in programming. In Python, we can use various functions and methods to handle input and output operations.

Input in Python

To get input from the user in Python, we can use the input() function. The input() function reads a line of text from the user and returns it as a string.

name = input("Enter your name: ")
print("Hello, " + name)

In the above example, the input() function prompts the user to enter their name, and the entered value is stored in the name variable. The print() function then displays a greeting message with the entered name.

Output in Python

To display output in Python, we can use the print() function. The print() function takes one or more arguments and displays them on the console.

print("Hello, World!")

In the above example, the print() function displays the message "Hello, World!" on the console.

We can also format the output using the format() method. The format() method replaces placeholders in a string with values.

name = "John"
age = 30
print("My name is {} and I am {} years old.".format(name, age))

In the above example, the format() method replaces the {} placeholders in the string with the values of the name and age variables.

We can also use f-strings to format the output. f-strings are a new feature in Python 3.6 and later versions.

name = "John"
age = 30
print(f"My name is {name} and I am {age} years old.")

In the above example, the f-string replaces the expressions inside the curly braces with their values.

About the author

William Pham is the Admin and primary author of With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.