Isalpha Method in Python

Free Video Tutorial and Guide

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.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

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 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 through false 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 a bunch of 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 equals true because method itself returns true, so in this case, you don't need so that would be redundant if you do equals 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 a bunch of 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 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.

photo of Noble Desktop

Noble Desktop

At Noble Desktop you can learn how to code websites, build iOS apps, make graphics, and more. From our front-end coding bootcamp to Photoshop classes, we offer a variety of comprehensive day, evening, and weekend training classes as well as certificate programs in web design, web development, HTML email, and more. Courses are available in-person (at our training facility in Soho), live online, and customized corporate or private training. Our course materials and workbooks are used by colleges and schools worldwide. Learn a skill today and start using it tomorrow.

More articles by Noble Desktop

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram