• Register
0 votes
20 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,050 points

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,930 points

Related questions

1 vote
1 answer 28 views
28 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 samhaz 5k points
0 votes
1 answer 23 views
23 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 LizzyM 6.1k points
1 vote
1 answer 20 views
20 views
Problem: I tried to check if a string is a palindrome or not so I wrote this program in python: def Word(str): for i in range(0, len(str)/2): if str[i] != str[len(str)-i-1]: return False return True a = input("Enter a word to check:") ... TypeError: 'float' object cannot be interpreted as an integer What should I do now and how to check if a string is a palindrome or not using python program?
asked May 15 samhaz 5k points
1 vote
1 answer 18 views
18 views
Problem: I wrote this code to check if a number has same number of set and unset bits: def IsSame(x):     set, unset = 16, 16     # iterate for all bits of a number     while(x):         # if set         if (x and 1):             set + 1               ... ) But when I run this or compile this program it never shows any output, my terminal is aways blank. What should I do now? Thanks i advance.
asked Jun 7 samhaz 5k points
0 votes
1 answer 12 views
12 views
I am new to python programming.. I was trying to check if a string is empty or whitespace in a code : Mystr= "" Yourstr = " " print ("This string is empty : ", end = "") if(not Yourstr): print ("Yes") print ("This string is empty : ", end = "") if(not Mystr): print ("No") It’s output is : This string is empty : This string is empty : No Its not giving me actual result what can I do now?
asked Mar 16 LizzyM 6.1k points