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
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.
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
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))
Hello world first value: 4 second value: 6 4+6 = 10
b, equal to 4 and 6, respectively.
endparameter in the first
b. The values of these variables are first converted to strings using the
strfunction, and then concatenated with other strings to form the final message. The
\ncharacters are used to insert a newline character in the output, causing the next line of text to be printed on a new line.
b. The value of
ais first converted to a string using the
strfunction, and the value of
bis converted to a string representation using the
reprfunction. These strings are then concatenated with the
+operator, and the resulting string is passed to the
variable-name = data-type(input("please enter something: "))
firstNumber = int(input("Please enter first number: ")) secondNumber = int(input("please enter second number: ")) result = firstNumber + secondNumber print(result)
Please enter first number: 4 Please enter second number: 8 12
inputfunction to prompt the user to enter two numbers, and store the user’s inputs in the variables
intfunction is used to convert the strings that are returned by the
inputfunction into integer values.
secondNumberand stores the result in the variable
resultvariable. 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.