473,325 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

checking if a string contains a number

Hi,
I have a string like,
s1 = '12e3'
s2 = 'junk'

Now before converting these values to float, I want to check if they
are valid numbers.

s1.isdigit returns False.

Is there any other function which would return True for s1 and False
for s2.

Thanks

--
Suresh
Dec 20 '05 #1
4 3510
You should probably work through the tutorials.

Use a try block:

try: x=float(s)
except ValueError:
print 'Non-numeric value %s found' % s

-Larry Bates

Suresh Jeevanandam wrote:
Hi,
I have a string like,
s1 = '12e3'
s2 = 'junk'

Now before converting these values to float, I want to check if they
are valid numbers.

s1.isdigit returns False.

Is there any other function which would return True for s1 and False
for s2.

Thanks

--
Suresh

Dec 20 '05 #2
On Tue, Dec 20, 2005 at 07:16:46PM +0530, Suresh Jeevanandam wrote:
s1 = '12e3'
s2 = 'junk'
Is there any other function which would return True for s1 and False
for s2.


isinstance(12e3, (int, float))
--
Egbert Bouwman - Keizersgracht 197 II - 1016 DS Amsterdam - 020 6257991
================================================== ======================
Dec 20 '05 #3
On Tue, 20 Dec 2005 19:16:46 +0530, Suresh Jeevanandam wrote:
Hi,
I have a string like,
s1 = '12e3'
s2 = 'junk'

Now before converting these values to float, I want to check if they
are valid numbers.
Just try converting them:

float_list = []
for s in string_list:
try:
float_list.append(float(s))
except ValueError:
# ignore bad strings
pass
do_something_with_floats(float_list)

s1.isdigit returns False.
"2" is a digit. "23" is two digits. You want something like s1.isfloat(),
but why bother checking first? Just Do It.
Is there any other function which would return True for s1 and False
for s2.


From an interactive interpreter, call dir(s1). That will give you a list
of string methods. Then call help(s1.method) to learn what that method
does.
--
Steven.

Dec 20 '05 #4
On Tue, 20 Dec 2005 15:55:37 +0100, egbert wrote:
On Tue, Dec 20, 2005 at 07:16:46PM +0530, Suresh Jeevanandam wrote:
s1 = '12e3'
s2 = 'junk'
Is there any other function which would return True for s1 and False
for s2.


isinstance(12e3, (int, float))


That won't work, because s1 is a *string*, not a float. The original
poster is asking how to check whether the string can be converted to a
float successfully before actually trying to convert it to a float.
--
Steven.

Dec 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tom Esker | last post by:
I've got Javascript in a form that adds up all of the numbers in a column of form fields and displays a total. It works great if every field has an initial value of 0, but if any of them are null,...
9
by: booksnore | last post by:
I am writing some code to search for strings that contain every letter of the alphabet. At the moment I am using the method below to check to see if a string contains every letter of the alphabet....
4
by: Patient Guy | last post by:
Does anyone have any coding rules they follow when doing argument checking? When arguments fail during check, do you return from the call with an ambiguous return value, or do you throw...
42
by: =?Utf-8?B?UGxheWE=?= | last post by:
I have an if statement that isn't working correctly and I was wondering how I check for a blank string. My Code Example if me.fieldname(arrayIndex) = "" then ----- end if When I do this and...
13
by: nishit.gupta | last post by:
Is their any fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" Thnx
11
by: Bryan Crouse | last post by:
I am looking a way to do error checking on a string at compile time, and if the string isn't the correct length have then have the compiler throw an error. I am working an embedded software that...
7
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I need to check to see if a string contains two different constraints. I currently use this else if statement to look for the first constraints; else if...
4
by: Hamish | last post by:
Hey I'm new to python, but I have used a fair bit of C and Perl I found Perls regex's to be very easy to use however I don't find Pythons regexes as good. All I am trying to do is detect if...
21
by: ningxin | last post by:
Hi, i am currently taking a module in c++ in the university, and was given an assignment. because i have no prior background on the subject, everything is kind of new to me. i have tried for quite...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.