This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add .isfloat() method to str
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.10
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: JimmyCarlos, eric.smith
Priority: normal Keywords:

Created on 2021-04-11 14:53 by JimmyCarlos, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg390785 - (view) Author: (JimmyCarlos) Date: 2021-04-11 14:53
Hello Python Community!

One feature I think would be helpful would be a method to see if a str can be safely converted into a float. Currently, this is not doable without a long regex or a try-except block, as numbers like "-3.52" contain non-numeric symbols.

My suggestion is to make a new boolean method on the str class. Code-wise, it would behave quite similarly to this code:

def isfloat(s:str) -> bool:
    try:
        float(s)
        return True
    except:
        return False


I appreciate your feedback, so what do you all think?
msg390789 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-04-11 17:24
Wouldn't the next thing you do be to convert it to a float, so you'd call float() twice? I think you'd be better off just catching the exception yourself, and using the result of float().

I'm opposed to such a simple function being a member of str or in the stdlib. In all of my years using Python with plenty of floats, I've never needed this function.

The usual suggestion with such short functions it to just add them to your own utility library.
History
Date User Action Args
2022-04-11 14:59:44adminsetgithub: 87974
2021-04-11 17:46:54gregory.p.smithsetstatus: open -> closed
resolution: rejected
stage: resolved
2021-04-11 17:24:54eric.smithsetversions: + Python 3.10
nosy: + eric.smith

messages: + msg390789

components: + Interpreter Core
2021-04-11 14:53:35JimmyCarloscreate