Given a string, write a Python program to find whether a string contains only numbers or not. Let’s see a few methods to solve the above task.
Check if String Contains Only Numbers
Check if String Contains Only Numbers using isdigit() method
Python String isdigit() method returns “True” if all characters in the string are digits, Otherwise, It returns “False”.
Python3
# Initialising string ini_string1 = '1234556' ini_string2 = 'ab123bc' # printing initial string print ( "Initial Strings : " , ini_string1, ini_string2) # Using isdigit() if ini_string1.isdigit(): print ( "String1 contains all numbers" ) else : print ( "String1 doesn't contains all numbers" ) if ini_string2.isdigit(): print ( "String2 contains all numbers" ) else : print ( "String2 doesn't contains all numbers" ) |
Output:
Initial Strings : 1234556 ab123bc String1 contains all numbers String2 doesn't contains all numbers
Time Complexity: O(n)
Auxiliary Space: O(1)
Check if String Contains Only Numbers using isnumeric()
Python String isnumeric() method returns “True” if all characters in the string are numeric characters, otherwise returns “False”.
Python3
# Initialising string ini_string1 = '1234556' ini_string2 = 'ab123bc' # printing initial string print ( "Initial Strings : " , ini_string1, ini_string2) # Using isnumeric() if ini_string1.isnumeric(): print ( "String1 contains all numbers" ) else : print ( "String1 doesn't contains all numbers" ) if ini_string2.isnumeric(): print ( "String2 contains all numbers" ) else : print ( "String2 doesn't contains all numbers" ) |
Output
Initial Strings : 1234556 ab123bc String1 contains all numbers String2 doesn't contains all numbers
Check if String Contains Only Numbers using regex.match
The re.match() searches only from the beginning of the string and returns the match object if found. But if a match of substring is found somewhere in the middle of the string, it returns none.
Python3
import re # Initialising string ini_string1 = '1234556' ini_string2 = 'ab123bc' # printing initial string print ( "Initial Strings : " , ini_string1, ini_string2) # Using regex() if re.match( '^[0-9]*$' , ini_string1): print ( "String1 contains all numbers" ) else : print ( "String1 doesn't contains all numbers" ) if re.match( '^[0-9]*$' , ini_string2): print ( "String2 contains all numbers" ) else : print ( "String2 doesn't contains all numbers" ) |
Output:
Initial Strings : 1234556 ab123bc String1 contains all numbers String2 doesn't contains all numbers
Check if String Contains Only Numbers using try/exception
This is also a simple program to test if a string contains an only a number or not.
Python3
# Initialising string ini_string1 = '1234556' ini_string2 = 'abc123' # printing initial string print ( "Initial Strings : " , ini_string1, ini_string2) # Using try / exception: try : num = int (ini_string1) print ( "String1 contains only digits" ) except : print ( "String1 doesn'tcontains only digits" ) try : num = int (ini_string2) print ( "String2 contains only digits" ) except : print ( "String2 doesn't contains only digits" ) |
Output:
Initial Strings : 1234556 abc123 String1 contains only digits String2 doesn't contains only digits
Check if String Contains Only Numbers using replace() method
Python3
# Python | Check whether string contains # only numbers or not # Initialising string ini_string1 = '12345h56' # printing initial string print ( "Initial String : " , ini_string1) digits = "0123456789" for i in digits: ini_string1 = ini_string1.replace(i, "") if len (ini_string1) = = 0 : print ( "String1 contains all numbers" ) else : print ( "String1 doesn't contains all numbers" ) |
Initial String : 12345h56 String1 doesn't contains all numbers
Time complexity: O(n), where n is length of ini_string1.
Auxiliary Space: O(1)
Check if String Contains Only Numbers using len() method
Python3
#Python | Check whether string contains only numbers or not # Initialising string ini_string1 = '1234556' # printing initial string print ( "Initial String : " , ini_string1) digits = "0123456789" c = 0 for i in ini_string1: if i in digits: c + = 1 if len (ini_string1) = = c: print ( "String1 contains all numbers" ) else : print ( "String1 doesn't contains all numbers" ) |
Initial String : 1234556 String1 contains all numbers
Check if String Contains Only Numbers Using operator.countOf() method
Python3
# Python | Check whether string contains only numbers or not import operator as op # Initialising string ini_string1 = '1234556' # printing initial string print ( "Initial String : " , ini_string1) digits = "0123456789" c = 0 for i in ini_string1: if op.countOf(digits, i) > 0 : c + = 1 if len (ini_string1) = = c: print ( "String1 contains all numbers" ) else : print ( "String1 doesn't contains all numbers" ) |
Initial String : 1234556 String1 contains all numbers
Time Complexity: O(N)
Auxiliary Space : O(1)