Input / output

To write output to the screen in Python, use the print() function. The data type of the output message is always converted to the data type string. Other data types like integer or float must therefore be converted to a string by the str() or repr() function.

str() and repr() are both functions used to get a string representation of an object.
str() is used to create the output for the end user, while repr() is mainly used for debugging and development. The str() Function thus returns a user friendly string representation of an object. The repr() Function returns a developer friendly description of the object.

Output Syntax

print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Output example

Code Explanation

LineDescription
1Initializes the variables a and b with the values 4 and 6
3 – 4Outputs the string Hello World
6 – 7Outputs the two values, and adds them together
a, b = 4, 6

print("Hello", end=" ")
print("world")

print("first value: " + str(4) + "\n" + "second value: " + str(b))
print(str(a) + "+" + repr(b) + " = " + str(a + b))
Output
Hello world
first value: 4
second value: 6
4+6 = 10

Input syntax

variable-name = data-type(input("please enter something: "))

Input example

Code Explanation

LineDescription
1 – 2Prompts the user to enter integer numbers.
4Adds the two values and stores them in the variable result
6Outputs result with print() function
firstNumber = int(input("Please enter first number: "))
secondNumber = int(input("please enter second number: "))

result = firstNumber + secondNumber

print(result)
Output
Please enter first number: 4
Please enter second number: 8
12