Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
an input question
#1
Hello everyone,
I just wanted to know how I can check if a string input is a number?
Reply
#2
You can try to convert the str into an int or float and catch the exception if it fails.
Reply
#3
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
Reply
#4
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
Reply
#5
Did you look through the docs for str , i.e. the list of methods?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  input function question barryjo 12 2,637 Jan-18-2022, 12:11 AM
Last Post: barryjo
Big Grin General programming question (input string)[ jamie_01 2 1,569 Jan-08-2022, 12:59 AM
Last Post: BashBedlam
Exclamation question about input, while loop, then print jamie_01 5 2,616 Sep-30-2021, 12:46 PM
Last Post: Underscore
  A question about subprocess taking input from command line and returning output! Aurimas 8 5,107 May-15-2019, 04:02 PM
Last Post: Aurimas
  User input question linuxnoob 9 4,842 Jul-12-2018, 01:56 PM
Last Post: linuxnoob
  Beginner question: help ensuring input is a number ycrad 2 2,869 Dec-23-2017, 10:49 PM
Last Post: snippsat
  Basic input() function question rebubula76 2 3,193 Nov-27-2017, 08:12 AM
Last Post: buran
  Input Question BlueberryCoconutMuffin 5 3,919 Oct-18-2017, 09:37 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020