Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge
  • All
  • C#
  • MVC
  • Web API
  • Azure
  • IIS
  • JavaScript
  • Angular
  • Node.js
  • Java
  • Python
  • SQL Server
  • SEO
  • Entrepreneur
  • Productivity

Convert Input to Number in Python

In Python 3.x, the input() function parse user input as a string even if it contains only digits.

Example: intput() User Inputs are String Objects
>>> import sys >>> data=input("Enter a Value: ") Enter a Value: 100 >>> data '100' >>> type(data) <class 'str'> >>> data=input("Enter a Value: ") Enter a Value: Hello >>> data 'Hello' >>> type(data) <class 'str'>

How do we ensure a numeric input from the user? Most common alternative is to parse return value of the input() function to integer with int() function

Example: Convert User Input to Int
>>> data=int(input("Enter a Number: ")) Enter a Number: 100 >>> data 100 >>> type(data) <class 'int'>

However, this is prone to error. If the user inputs non-numeric data, ValueError is raised.

Example: Convert User Input to Int
>>> data=int(input("Enter a Number: ")) Enter a Number: hello Traceback (most recent call last): File "<pyshell#34>", line 1, in <module> data=int(input("Enter a Number: ")) ValueError: invalid literal for int() with base 10: 'hello'

This can be taken care of by Python's exception handling technique. The following code keeps on asking for user input till an integer number is given.

Example: Convert User Input to Int
while True: try: data=int(input("Enter a Number: ")) print ("You entered: ", data) break; except ValueError: print ("Invalid input")
Output
Enter a Number: hello Invalid input Enter a Number: abcd Invalid input Enter a Number: 100 You entered: 100

You can use the built-in float() function if a floating-point number is expected to be input.

Another method is to use the eval() function. Apart from other applications of this built-in function, it is a convenient tool to check if the input is a valid number. In case it is not, the Python interpreter raises NameError

Example: Convert User Input to Number
while True: try: data=eval(input("Enter a Number: ")) print ("You entered: ",data) break; except NameError: print ("Invalid input")
Output
Enter a Number: hello Invalid input Enter a Number: abcd Invalid input Enter a Number: 12.34 You entered: 12.34

Convert Input to Number in Python 2.x

Python 2.x has two built-in functions for accepting user input. the raw_input() and input(). The input() function is intelligent as it judges the data type of data read, whereas the raw_input() always treats the input as a string. So, always use the input() function in Python 2.x.

Example: Convert User Input to Int in Python 2
>>> data=input("enter something : ") enter something : 100 >>> data 100 >>> type(data) <type 'int'> >>> data=input("enter something : ") enter something : Hello' >>> data 'Hello' >>> type(data) <type 'str'>
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.