How do you print numbers in python?


  • Theory
  • Steps
  • Problems

1. How to read and write in Python

Every program is eventually a data processor, so we should know how to input and output data within it. There exists a function, print(), to output data from any Python program. To use it, pass a comma separated list of arguments that you want to print to the print() function. Let's see an example. Press "run" and then "next" to see how the program is being executed line by line:

print(5 + 10)
print(3 * 7, (17 - 2) * 8)
print(2 ** 16)  # two stars are used for exponentiation (2 to the power of 16)
print(37 / 3)  # single forward slash is a division
print(37 // 3)  # double forward slash is an integer division
        # it returns only the quotient of the division (i.e. no remainder)
print(37 % 3)  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value

To input data into a program, we use input(). This function reads a single line of text, as a String.

Here's a program that reads the user's name and greets them:

print('What is your name?')
name = input()  # read a single line and store it in the variable "name"
print('Hi ' + name + '!')
    

Advertising by Google, may be based on your interests

2. Sum of numbers and strings

Let's try to write a program that inputs two numbers and prints their sum. We read the two numbers and store them in the variables a and b using the assignment operator =. On the left side of an assignment operator we put the name of the variable. The name could be a string of latin characters (A-Z, a-z, 0-9, _) but must start with a letter in the range A-Z or a-z. On the right side of an assignment operator we put any expression that Python can evaluate. The name starts pointing to the result of the evaluation. Read this example, run it and look at the output:

a = input()
b = input()
s = a + b
print(s)
    

After running the example we can see that it prints 57. As we were taught in school, 5 + 7 gives 12. So, the program is wrong, and it's important to understand why. The thing is, in the third line s = a + b Python has "summed" two strings, rather than two numbers. The sum of two strings in Python works as follows: they are just glued one after another. It's also sometimes called "string concatenation".

Do you see in the variable inspector, on the right hand side, that the values bound to variables a and b are wrapped in quotes? That means that the values there are string, not numbers. Strings and numbers are represented in Python differently.

All the values in Python are called "objects". Every object has a certain type. The number 2 corresponds to an object "number 2" of type "int" (i.e., an integer number). The string 'hello' corresponds to an object "string 'hello'" of type "str". Every floating-point number is represented as an object of type "float". The type of an object specifies what kind of operations may be applied to it. For instance, if the two variables "first" and "second" are pointing to the objects of type int, Python can multiply them. However, if they are pointing to the objects of type str, Python can't do that:

first = 5
second = 7
print(first * second)

# you can use single or double quotes to define a string
first = '5'
second = "7"
print(first * second)
    

To cast (convert) the string of digits into an integer number, we can use the function int(). For example, int('23') gives an int object with value 23.

Given the information above, we can now fix the incorrect output and output the sum of the two numbers correctly:

a = int(input())
b = int(input())
s = a + b
print(s)
    

Advertising by Google, may be based on your interests

Using print function without parentheses works with older versions of Python but is no longer supported on Python3, so you have to put the arguments inside parentheses. However, there are workarounds, as mentioned in the answers to this question. Since the support for Python2 has ended in Jan 1st 2020, the answer has been modified to be compatible with Python3.

You could do any of these (and there may be other ways):

(1)  print("First number is {} and second number is {}".format(first, second))
(1b) print("First number is {first} and number is {second}".format(first=first, second=second)) 

or

(2) print('First number is', first, 'second number is', second) 

(Note: A space will be automatically added afterwards when separated from a comma)

or

(3) print('First number %d and second number is %d' % (first, second))

or

(4) print('First number is ' + str(first) + ' second number is' + str(second))
  

Using format() (1/1b) is preferred where available.

How do I print numbers from 1 to 10 in Python?

# Python program to print numbers from n to 1..
number = int ( input ( "Please Enter any Number: " )) i = number..
while ( i > = 1 ):.
print (i, end = ' ' ) i = i - 1..

How do I print 12345 in Python?

how to print 12345 in one row without using string.
+8. print(*(n for n in range(1,6)), sep="") or just print(12345) as suggested by Diego. ... .
+6. Language - Python This might be cheating but try it: num = 12345; print(num) And next time please specify the programming language. ... .
+5. ... .
+3. ... .
+2. ... .
+2. ... .
+1. ... .

How do I print numbers in a string in Python?

Use the syntax print(int("STR")) to return the str as an int , or integer. How to Convert Python Int to String: To convert an integer to string in Python, use the str() function.

How do you print 2 numbers in Python?

How to Add Two Numbers in Python.
❮ Previous Next ❯.
Example. x = 5. y = 10. print(x + y) Try it Yourself ».
Example. x = input("Type a number: ") y = input("Type another number: ") sum = int(x) + int(y) print("The sum is: ", sum) Try it Yourself ».
❮ Previous Next ❯.