1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How do I check if a string represents a number (float or int)?

Discussão em 'Python' iniciado por Stack, Dezembro 1, 2025.

  1. Stack

    Stack Membro Participativo

    How do I check if a string represents a numeric value in Python?

    def is_number(s):
    try:
    float(s)
    return True
    except ValueError:
    return False


    The above works, but it seems clunky.


    Editor's note: If what you are testing comes from user input, it is still a string even if it represents an int or a float. For converting the input, see How can I read inputs as numbers? For ensuring that the input represents an int or float (or other requirements) before proceeding, see Asking the user for input until they give a valid response

    Continue reading...

Compartilhe esta Página