Curriculum
Python provides built-in functions for handling input (getting data from users) and output (displaying data on the screen). These functions allow interaction between the user and the program.
This section covers:
input()
function to get user inputprint()
function to display outputBy the end of this lesson, you will be able to create interactive Python programs that accept input from users and display formatted output.
input()
The input()
function allows a program to receive user input as a string.
input()
name = input("Enter your name: ")
print("Hello,", name)
input()
function displays the prompt ("Enter your name: "
).name
as a string.print()
function displays a greeting using the input value.Enter your name: Alice
Hello, Alice
By default, input()
returns a string. If you need a number, convert the input using int()
or float()
.
age = input("Enter your age: ")
age = int(age) # Convert string to integer
print("Next year, you will be", age + 1)
Enter your age: 25
Next year, you will be 26
height = float(input("Enter your height in meters: "))
print("Your height is", height, "meters.")
Enter your height in meters: 1.75
Your height is 1.75 meters.
If the user enters non-numeric characters, Python will raise a ValueError. To prevent this, always validate input before conversion.
print()
The print()
function is used to display output. It can print strings, numbers, and variables.
You can print multiple values by separating them with commas (,
).
name = "Alice"
age = 25
print("Name:", name, "Age:", age)
Name: Alice Age: 25
Python automatically adds a space between values when using commas.
sep
to Customize SeparatorsBy default, print()
separates values with a space. You can change this using sep
.
print("Python", "is", "awesome", sep="-")
Python-is-awesome
end
to Control Line EndingsBy default, print()
adds a newline (\n
) at the end. You can change this using end
.
print("Hello", end=" ")
print("World!")
Hello World!
F-strings (f""
) allow inserting variables directly into strings.
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
My name is Alice and I am 25 years old.
.format()
for Formatted OutputThe .format()
method is an alternative to f-strings.
name = "Alice"
age = 25
print("My name is {} and I am {} years old.".format(name, age))
My name is Alice and I am 25 years old.
You can take multiple inputs in a single line using .split()
.
name, age = input("Enter your name and age: ").split()
print(f"Name: {name}, Age: {age}")
Enter your name and age: Alice 25
Name: Alice, Age: 25
Here, .split()
separates input values based on spaces.
If you need a comma-separated input, specify ","
in .split()
:
name, age = input("Enter your name and age separated by a comma: ").split(",")
print(f"Name: {name}, Age: {age}")
Here is a summary of what we have covered in this section
input()
is used to get user input. By default, it returns a string.int()
, float()
when working with numbers.print()
displays output and supports multiple values.sep
and end
to customize print behavior.f""
) or .format()
for formatted output..split()
allows multiple inputs on a single line.In the next exercise, you will apply what you have learned by writing a Python program that takes user input and displays formatted output.
Not a member yet? Register now
Are you a member? Login now