Check if value is string or number

Asked

Viewed 2,637 times

-2

10) Make an algorithm that checks whether a given value is a String.

valor = str(input("Informe um valor: "))

if(type(valor) == str):
    print("É uma String!")

In Pycharm IDLE or Python Console, type:

valor = "João"
type(valor)

he returns:

<class 'str'>

I Googled and heard about a function isinstance()

11) Make an algorithm that checks whether a given value is of the decimal type.

valor = input("Informe um valor: ")

if(isinstance(valor, float)):
    print("É um decimal!")
else:
    print("Não é um decimal!")

The above code of question 11 results in a wrong output, for if I type 3, or 3.2, in both it returns "It is not a decimal!"

I would like to know the answers to these 2 questions.. (use Python 2.7 or Python 3.7, both are in Virtual Environments with Anaconda)

I managed to do 14 exercises of 18, but these 2 and more other 2 that maybe I post later, I could not.

  • input always returns a string. To check if this string contains a value that represents a number, you use something like this: https://answall.com/q/210010/112052

3 answers

1

The above code of question 11 results in a wrong output, for if I type 3, or 3.2, in both it returns "It is not a decimal!"

The code depicts what is happening - the return value of the input in Python 3 at all times is a string.

To check whether the string can be used as an integer or a decimal number, you have to write more code. For example, strings have the method .isdigit() that returns True if all the characters in the string are digits - this allows checking whether the string contains an integer.

You can also use the method .count and check that the character count "." (or "," as you prefer) inside the string is 1. The most practical way to check if it is a valid float number however is to try to convert it to float, and capture a possible error, with a Try/except - if an error occurs, is because it is not a valid float.

a = input("digite coisas:")
try:
   b = float(a)
except ValueError:
   eh_decimal = False
else:
   eh_decimal = True

...

Maybe you got confused with some example you saw of Python 2, the input tries to interpret what is typed as a Python expression. Then numbers are returned directly from the input as "int" and "float" (and text will usually give an error ) . In Python 2, the correct one was to use raw_input in place of input.

  • Gee... Did that part of the code exist when I gave my answer? If yes I’m really freaking out. :-)

1


Considering only the decimal system (without taking into account hexa, octal, etc):

valor='1'
while valor!='0':
    valor = input('Digite o valor:')
    try:
        print(float(valor),' É um numero')
    except ValueError:
        print(valor, 'É uma string')

Obs.:
With a little bit of effort vc can refine more to distinguish float integers.

See working on repl it.

0

Try this

valor = "Joao"
if type(valor) is str:
    print("é string")
  • Hello @deshPro. Try to further detail your answer. Maybe explain briefly what makes your solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.