Want to check whether the string you have got only possesses digits in it or also has something else in it?
Then the process is very simple. And, don’t worry, we are not going to deal with so many lines of code. Rather, we will be using just a single word in code .isdigit() and guess what our work will be all done.
Yes, just one word will tell us what the content of our string is? That’s the power of Python’s built-in methods.
Let’s get into some more detail about this str.isdigit() method.
What is .isdigit() in Python? And it’s Syntax
str.isdigit() is a simple way that performs the task of checking the content of a string, and it returns True (internally) when the string only has numeric values in it.
In short, it’s a way of asking, Hey Python, I’ve got a new string. Now you go and check if it’s fully numeric or not? If it’s numeric, then only execute the further program; otherwise, stop there.
It’s syntax—- str.isdigit()
In order to use this method, the very basic requirement is “string,” whose content is to be verified. Just store that string in any variable and pass that variable in the syntax in place of str.
For example—-
Let’s say you have a string as “45678.” . Just store it in any variable, for instance my_string.
Then, this is how its code looks like.
my_string= “45678”
my_string.isdigit()
That’s how easy it is. We have just replace the str from syntax with the variable name and left everything was same. Right?
If you were expecting anything on screen as output, then let me tell you—this program is not going to print any output on the screen or console. But that doesn’t mean there’s something wrong with this code.
Here, the str.isdigit() method will only check and verify whether the string contains numeric values or not. This is its only and only job.
At this point in time, Python is like
I know. But I won’t tell. Fun aside
Therefore, we always try to use this type of method with other Python methods such as print() and conditional statements() that actually return some visual output about what has taken place.
In this way, we are able to use the full potential of passive methods like str.isdigit() by combining them with other methods.
How to use the .isdigit() method with if and else statements with example
Let’s try to use the .isdigit() method with conditional statements to see whether it actually works or not.
I hope you’re all familiar with conditional statements of python.
If you’re not yet aware about how if and else statements work then let me give you a quick intro about them.
If and else statements are very simple.
if-else in Python is like choosing between two options based on a question. If the answer is “yes,” you pick one thing; if “no,” you pick the other.
If it still sounds confusing then don’t stress yourself. We will now see its real use case with the .isdigit() method.
Here our string is “45678”
As we can see that our string only contains numbers so its output will be the first print() statement which is under the if block.
The string contains only digits.
If the string contains any non-digit characters (e.g., “123a”), the method will return False, and the output will be: else block
The string does not contain only digits.
That’s how .isdigit() method and Conditional statements can be made work together to give actual results.
One Comment