Input / output in Python

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

a, b = 4, 6

print("Hello", end=" ")

print("first value: " + str(4) + "\n" + "second value: " + str(b))
print(str(a) + "+" + repr(b) + " = " + str(a + b))
Hello world
first value: 4
second value: 6
4+6 = 10
Code Explanation
  1. The first line of code sets two variables, a and b, equal to 4 and 6, respectively.
  2. The next two print statements are used to print the string “Hello” followed by the string “world”. The end parameter in the first print statement is set to a space character, so the two strings will be printed on the same line with a space between them.
  3. The third print statement is used to print a message that includes the values of the variables a and b. The values of these variables are first converted to strings using the str function, and then concatenated with other strings to form the final message. The \n characters are used to insert a newline character in the output, causing the next line of text to be printed on a new line.
  4. The final print statement is used to print a message that includes the result of adding the values of a and b. The value of a is first converted to a string using the str function, and the value of b is converted to a string representation using the repr function. These strings are then concatenated with the + operator, and the resulting string is passed to the print function to be displayed.

Input syntax

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

Input example

firstNumber = int(input("Please enter first number: "))
secondNumber = int(input("please enter second number: "))

result = firstNumber + secondNumber

Please enter first number: 4
Please enter second number: 8
Code Explanation
  1. The first two lines of code use the input function to prompt the user to enter two numbers, and store the user’s inputs in the variables firstNumber and secondNumber. The int function is used to convert the strings that are returned by the input function into integer values.
  2. The third line of code adds the values stored in firstNumber and secondNumber and stores the result in the variable result.
  3. The final line of code uses the print function to display the value stored in the result variable. This will be the sum of the two numbers entered by the user.

In summary, this code prompts the user to enter two numbers, adds them together, and displays the result.