• Register
0 votes
26 views

Can I represent a string as number in python or how can I check if there is any number in string of python function.
Supose a function is:
 

def My_num(x):
      try:
             float(x)
             return True
       except ValueError:
             return False

my question is calling float in the main function is ok or not and I am not getting my expected result. 

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

0 votes

If you are looking for a parsing int instead of floats you can try isdigit() function as string objects.
e.g:
 

x = "74897"
x.isdigit()
True
y = "7687wa"
y.isdigit()
False

For different approach and accurate result you can use the code:

def My_number_tryexcept(x):
    """ Returns True if string is a number. """
    try:
        float(x)
        return True
    except ValueError:
        return False

import re    
def My_number_regex(x):
    """ Returns True is string is a number. """
    if re.match("^\d+?\.\d+?$", x) is None:
        return x.isdigit()
    return True


def My_number_repl_isdigit(x):
    """ Returns True if string is a number. """
    return x.replace('.','',1).isdigit()

Try except method that handels scientific notations correctly

funcs = [
          My_number_tryexcept, 
          My_number_regex,
          My_number_repl_isdigit
          ]

x_float = '.1234'

print('Float notation ".1234" is not supported by:')
for f in funcs:
    if not f(x_float):
        print('\t -', f.__name__)

:Let me know if it is working or not.

11 6 4
34,950 points

Related questions

1 vote
1 answer 29 views
29 views
Problem: I was trying to write code, in which I’ll get someone's name and age but I keep getting this error: 'int' object is not subscriptable error. Here is my code name = raw_input("What's your name? ") age = raw_input ("how old are you? ") x = 0 int([x[age]]) twentyone = 21 - x print "Hello, " + name+ " you will be 21 in: " + twentyone + " years." Any clues?
asked Mar 6, 2020 Gavin 15.3k points
1 vote
1 answer 3 views
3 views
Problem: Can anyone tell me the solution of this problem? python check if a string is a number
asked Mar 31 MUHAMMAD MUNEEB 83k points
1 vote
1 answer 4 views
4 views
Problem: Can someone please help fix this uncaught error : python check if string is a number
asked Mar 30 MUHAMMAD MUNEEB 83k points
1 vote
1 answer 4 views
4 views
Problem: I need a help , How I can fix this : python check if string is number
asked Mar 30 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 3 views
3 views
Problem: I am getting this .. can you help me to solve it? How to check if input is a number python
asked May 1 Ifra 37.2k points
0 votes
1 answer 6 views
6 views
Problem: I need someone’s assistance to solve my problem .. Check if a number is in a list python
asked Apr 27 Ifra 37.2k points
0 votes
1 answer 10 views
10 views
Problem: How to check if a number already exist in a list in python?
asked Apr 14 Humaira ahmed 50.7k points
0 votes
1 answer 14 views
14 views
Problem: how to I Solve this: check if a number is prime python HELP! I need help understanding this: check if a number is prime python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 8 Mashhoodch 13k points
1 vote
1 answer 55 views
55 views
Problem: I wrote a code to check if a number is even or odd in python. The program is in the snippet below: number = input("Please enter any number to check if it is even or odd: ") result = number % 2 if result == 0: print("The number ... 2 TypeError: not all arguments converted during string formatting Why this error message is appearing & what is the problem with my program? Thanks in advance.
asked May 13, 2020 samhaz 5k points
0 votes
1 answer 33 views
33 views
How can i check if a number is even in python? I am new to programming and I was doing some practice. I wrote a code to check if a number is even in python: number = int(input("Please enter a number to check")) if (number % ) == 1 print("the number is even") but ... an error : File "main.py", line 2 if (number % ) == 1 ^ SyntaxError: invalid syntax can anyone help me out to sort out this problem?
asked Mar 14, 2020 LizzyM 6.1k points