Gain a deeper understanding of how to use Python's isalpha() and isdigit() methods effectively for string manipulation tasks. Learn how these methods can help you analyze and manipulate strings, whether you're counting characters or determining if a string contains numbers or letters.
Key Insights
- Python's isalpha() method is used to check if a string or a substring is made up of alphabetical characters. If it is, the method returns true.
- The isdigit() method, on the other hand, checks if a string or a substring is composed of numerical characters. It returns true if the string contains only numbers.
- If a string has a mix of numbers and letters, both the isalpha() and isdigit() methods will return false. This is because these methods check if the entire string is made of either letters or numbers.
- These methods can be used in conjunction with for loops to analyze strings. For example, you can count the number of letters or numbers in a string.
- To count the occurrences of specific characters beyond just letters and numbers, conditional statements can be used in combination with these methods.
- The Python instructions provided in the video are delivered by a Noble Desktop instructor demonstrating practical applications of Python methods isalpha() and isdigit(), offering insights beneficial for both beginners and seasoned Python programmers.
In this video, we're going to look at how to use the Isalpha Method in Python
Video Transcription
Hi, my name is Art, and I teach Python at Noble Desktop. In this video, I'm going to show you how to use the methods isalpha() and isdigit().
So, first thing first, suppose we have a string, "Apple". Now, remember before I showed you in my videos, I showed you how to use the function dir(), and I could pass here a word, and that will get me all the methods that you can find here. So, let's take a look.
The methods I'm talking about are isalpha() and isdigit()—here they are. So, what's the difference? Now, if you don't know what they do, that's okay because I hope you remember how to use help. So, isalpha()—if we run it like this, it will get us true.
Opposite of isalpha() there is isdigit(). Isdigit() will tell you true or false if what you have in the string looks like numbers. So, it's false because obviously here we have alphabetical characters, but if I replace them with numbers, now you see that would be false and this digit will return as true.
That's the whole idea. So, you could actually identify what you have there. However, in some instances, maybe you got a mixture of all letters and numbers. Now, in this case, both will return false, why? Because that's neither one of them because it's looking for the full string full of letters or numbers.
Now, what we could do in this case is also very popular exercise—we could count how many letters and how many numbers do we have in the string. So, how are we going to do it? First thing first, we need to grab each letter or let me say each character and apply this to each character.
So, how are we going to do it? We need to use a for loop. Let me call this char in the word. And again, when you program, do not rush—go baby steps. Let's see print Char—let's see what Char is. Apparently, Char is each character right.
So, now what we could do is actually apply isalpha() and now you see we're getting through false. If you want to count them, we need to create counters. So, let me call it counter letters and that would be zero at the beginning. And let's create counter numbers.
Alright, so in here, I wanna increase these count letters if this returns true and you see I'm getting many truths here. So, what I'm going to do is wrap this in an if statement and that's going to be my condition. And keep in mind, you don't need to do this equals true because method itself returns true, so in this case, you don't need so that would be redundant if you do equals true. So, you could just leave it like this.
And then what I want to do is I want to take this and add one if it's true. I won't be doing else, why? Because later, we want to do something else—I'll show you where I'm getting with that. So, in this case, I'm going to use an if and I'm going to create a new condition—char isdigit()—alright, his digit.
So, now you see what I'm going to do with this digit—if it returns true, I will increase it by one. So, let's stop right here and let's print count letters and count numbers. So, if you run it, apparently, in this stream, we have five letters and six numbers. And you might say, "Hey, but why didn't we use else?" Let me show you something.
So, suppose you got many characters and now you need to count those. Right? So, we could use else or again, we could be very specific and we could create another counter. So, then if you want to search for a specific counter, you could do another if statement and then you could do what? Now, there is no function for that—how will we do it? So, it's going to be character equals and we're looking for a specific character. Right? And then what I'm going to do is I'm just going to take this counter and I could update this counter y1 and also could place it here.
So, that would be—for example, if you don't have a method, so you could count and apparently we have eight ads in this example. So, isalpha() will return true if string or substring is alphabetical character. Isdigit() will return true if string a substring is number from 0 to 9.