Check Whether String Contains Only Numbers or Not - Python Last Updated : 28 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report We are given a string s="12345" we need to check whether the string contains only number or not if the string contains only number we will return True or if the string does contains some other value then we will return False. This article will explore various techniques to check if a string contains only numbers including simple string methods regular expressions, and other useful approaches.Using str.isdigit()str.isdigit() method in Python is a straightforward way to check if a string contains only numeric characters, this method is especially useful for validating input or extracting numeric data from strings during data processing. Python s = "12345" # Check if the string contains only numbers b = s.isdigit() print(b) OutputTrue Explanation:str.isdigit() checks whether all characters in the string are numeric returning True if the string contains only digits and False otherwise.In this example since the string "12345" consists solely of numeric characters, str.isdigit() returns True, confirming the string is entirely numeric.Using Regular Expressions (re.fullmatch)re.fullmatch() method is a powerful way to verify if an entire string follows a specific pattern, it allows precise control over pattern matching such as ensuring a string contains only numeric characters. Python import re s = "12345" # Check if the string contains only numbers using regex b = bool(re.fullmatch(r'\d+', s)) print(b) OutputTrue Explanation:Code uses re.fullmatch(r'\d+', s) to check if the entire string s matches the regular expression \d+, which ensures the string contains one or more digits (\d represents a digit).Result of re.fullmatch is converted to a boolean using bool(), returning True if the string contains only numbers and False otherwiseUsing str.replace()str.replace() method in Python allows us to replace specific characters or substrings within a string with another value. Python s = "123.45" # Check if the string contains only numbers and a single decimal point b = s.replace('.', '', 1).isdigit() print(b) OutputTrue Explanation:str.replace() is used to remove the first occurrence of a decimal point (.) from the string, allowing strings with only one decimal point to pass validation. After this, it checks if the remaining string is numeric using isdigit().If the modified string contains only digits, the isdigit() method returns True indicating the input string represents a valid numeric value (with at most one decimal point).Using all()To check if a string contains only digits or numeric values, we can use the all() function combined with str.isdigit() Python s = "12345" # Check if all characters are digits b = all(char.isdigit() for char in s) print(b) OutputTrue Explanation:all() checks if all elements in an iterable are True. In this case, it verifies if every character in the string is a digit using the isdigit() method.If all characters in the string are digits it returns True, otherwise it returns False. In this case, since "12345" consists entirely of digits, the result is True Comment More infoAdvertise with us Next Article Python | Ways to check if given string contains only letter G garg_ak0109 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python - Check if String contains any Number We are given a string and our task is to check whether it contains any numeric digits (0-9). For example, consider the following string: s = "Hello123" since it contains digits (1, 2, 3), the output should be True while on the other hand, for s = "HelloWorld" since it has no digits the output should 2 min read Python | Check whether a string is valid json or not Given a Python String, the task is to check whether the String is a valid JSON object or not. Let's try to understand the problem using different examples in Python. Validate JSON String in PythonThere are various methods to Validate JSON schema in Python here we explain some generally used methods 3 min read Python regex | Check whether the input is Floating point number or not Prerequisite: Regular expression in Python Given an input, write a Python program to check whether the given Input is Floating point number or not. Examples: Input: 1.20 Output: Floating point number Input: -2.356 Output: Floating point number Input: 0.2 Output: Floating point number Input: -3 Outpu 2 min read Python | Ways to check if given string contains only letter Given a string, write a Python program to find whether a string contains only letters and no other keywords. Let's discuss a few methods to complete the task. Method #1: Using isalpha() method Python3 # Python code to demonstrate # to find whether string contains # only letters # Initialising string 3 min read Python Program to test if the String only Numbers and Alphabets Given a String, our task is to write a Python program to check if string contains both numbers and alphabets, not either nor punctuations. Examples: Input : test_str = 'Geeks4Geeks' Output : True Explanation : Contains both number and alphabets. Input : test_str = 'GeeksforGeeks' Output : False Expl 4 min read Python Program to check whether all elements in a string list are numeric Given a list that contains only string elements the task here is to write a Python program to check if all of them are numeric or not. If all are numeric return True otherwise, return False. Input : test_list = ["434", "823", "98", "74"] Output : True Explanation : All Strings are digits.Input : tes 5 min read Like