Python | Typecasting Input to Integer, Float

Here, we are going to learn how to typecast given input to integer, float in Python? By IncludeHelp Last updated : April 08, 2023

To input any value, we use input() function - which is an inbuilt function.

Typecasting string input to integer

For typecasting string input to integer, we use int() function, it accepts a string value and returns an integer value.

Syntax

int(input())

Example for typecasting string input to integer

# input a number
num = int(input("Input a value: "))

# printing input value 
print "num = ", num

Output

Input a value: 10
num =  10

Typecasting string input to float

For typecasting string input to float, we use float() function, it accepts a string value and returns a float value.

Syntax

float(input())

Example for typecasting string input to float

# input a number
num = float(input("Input a value: "))

# printing input value 
print "num = ", num

Output

Input a value: 10.23
num =  10.23

Python Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.