• Register
0 votes
23 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 39 views
39 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 28 views
28 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
0 votes
1 answer 19 views
19 views asked Oct 29, 2020 john 580 points
1 vote
1 answer 26 views
26 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, 2020 samhaz 5k points
0 votes
1 answer 4 views
4 views
Problem: Python String isnumeric() Method The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False. Exponents, like ² and ¾ are also considered to be numeric values. then how check if a string is a number python
asked 13 hours ago Mashhoodch 9.1k points
1 vote
1 answer 22 views
22 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, 2020 samhaz 5k points
0 votes
1 answer 19 views
19 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, 2020 LizzyM 6.1k points
0 votes
1 answer 12 views
12 views
I need to check if a number is a perfect squre in java.
asked Oct 5, 2020 Daniel Anderson 4k points
1 vote
1 answer 50 views
50 views
Problem: I wrote a program in python to check if a given number is sparse or not. So I wrote this code: def IsSparse(n): if ((n<<1) & (n>>1)): return 0 return 1 print(IsSparse(35)) print(IsSparse(16)) print(IsSparse(12)) print(IsSparse(72)) And when I run this or ... . The output is: 1 1 1 1 But the actual output should be: 0 1 0 1 What should I do to get the actual result? Thanks in advance.
asked Apr 22, 2020 samhaz 5k points
1 vote
1 answer 24 views
24 views
Problem: I was trying to write code, in which I&rsquo;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