Jul-14-2020, 11:42 AM
Hello everyone,
I just wanted to know how I can check if a string input is a number?
I just wanted to know how I can check if a string input is a number?
an input question
|
Jul-14-2020, 11:42 AM
Hello everyone,
I just wanted to know how I can check if a string input is a number?
Jul-14-2020, 11:47 AM
You can try to convert the str into an
int or float and catch the exception if it fails.
Jul-14-2020, 11:47 AM
Hello,
there are several ways. There are plenty of explanations available if you search online. You can also start with this tutorial on our forums: https://python-forum.io/Thread-Validating-User-Input
Jul-14-2020, 02:15 PM
As the above post said, there are several ways, one of which is to use the variable.is_integer()
var=input("Enter a value") if var.isint() == True: return True else: return False
Did you look through the docs for
str , i.e. the list of methods?
|
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
input function question | barryjo | 12 | 4,492 |
Jan-18-2022, 12:11 AM Last Post: barryjo |
|
![]() |
General programming question (input string)[ | jamie_01 | 2 | 2,311 |
Jan-08-2022, 12:59 AM Last Post: BashBedlam |
![]() |
question about input, while loop, then print | jamie_01 | 5 | 3,753 |
Sep-30-2021, 12:46 PM Last Post: Underscore |
A question about subprocess taking input from command line and returning output! | Aurimas | 8 | 6,500 |
May-15-2019, 04:02 PM Last Post: Aurimas |
|
User input question | linuxnoob | 9 | 6,402 |
Jul-12-2018, 01:56 PM Last Post: linuxnoob |
|
Beginner question: help ensuring input is a number | ycrad | 2 | 3,567 |
Dec-23-2017, 10:49 PM Last Post: snippsat |
|
Basic input() function question | rebubula76 | 2 | 3,865 |
Nov-27-2017, 08:12 AM Last Post: buran |
|
Input Question | BlueberryCoconutMuffin | 5 | 4,930 |
Oct-18-2017, 09:37 PM Last Post: nilamo |