Python - Output and Input

Input

To enter values into a program, we first need a variable to store the data. We then assign a value returned by the input command.

print("Enter your name:")

name = input()

The input command always returns a value that is stored as a string datatype. If we want to store numeric values we must always convert (or cast) the string into the correct datatype.

To store an integer value we use the int(...) function:

print("Enter an integer value:")

number = int(input())

To store an real value we use the float(...) function:

print("Enter an real number:")

number = float(input())

Watch the video below to see how to enter and assign data of different types to a variable

pythonInput.mp4

Output

Literal values are output using the print command as follows:

print("hello")

print(7)

Values stored in variables are output as follows:

name = "Bob"

age = 12

print(name)

print(age)

Several values can be output using the same command, when separated by commas. This is called concatenation.

name = "Alice"

age = 13

print(name, " is ", age, " years old.")

Key Words

Cast

To convert the data type of data (for example an input string into an integer).

Read more about casting.

Concatenate

To joint together two strings.