Integers and strings | SoloLearn: Learn to code for FREE!

+1

Integers and strings

Why is the int sign ever needed? What is an example of when I would need that in a string? Could I not just make the numbers I wanted outside of the string.

6/14/2019 5:17:05 AM

Maggie Jones

5 Answers

New Answer

+2

age = int(input("Enter your age: "))

+2

num1 = int(input()) num2 = int(input()) print(num1 + num2)

+2

for i in range(int(input("How many times will the loop be run? "))):

+2

Maggie Jones Default input in python is string. If you enter 2 nums like 3 and 5, python takes them as strings. Then doing num1 = input() // 3 num2 = input() // 5 num1 + num2 would do string concatenation and produce 35 as result not 8 as you would be expecting. so to convert the input to appropriate number (int or float) you need explicit cast num1 = int(input()) // 3 num2 = int(input()) // 5 now if you do num1 + num2 the answer would be 8 due to integer addition

+1

If you want to multiplicate something, that can't be multiplicated with floats, such as lists, [6, 3, 1]*int(5.5) => [6, 3, 1]*5. You can also test whether a float is a whole number by testing int(x) == x