Python challenges: String type counter

Transcript

English (Auto-generated)

Hello everyone. And how is everybody doing? So today we're back with another python challenge. Um Once again it's a fairly simple one. We're going to be given a string. Um with a bunch of random letters characters and stuff like that and we have to return the number of uppercase letters. Lowercase letters numbers or integers. Um And special characters. So so it's definitely pretty easy. So let's start doing that. So the first thing we're gonna do is create variables where we're gonna be storing the number of each type. So we're gonna have one for uppercase letters, lowercase letters etcetera. Okay so it's there. Okay and I also have one it's numb and special zero right here. Okay perfect And we're gonna want to read through each part of the string so we're gonna do four I. N. S. Listen let me just change it to char as well for character and s ready? Check if char that is upper which is really technique. If it's an uppercase letter um equals Mhm. Yes sir that is upper equals true. Then we're going to add 1-plus equals to our upper counter. Cool. Ellis LF char. That is lower and we quote if it equals true then lower. Now plus equals one. Oh okay and another LF statement. Um LF char that is numerically but also to actually from equals true then numb plus equals one and else if there's anything aside from that then it has to be a special character at this point. So then we're just gonna plus equals +12 special characters. And then what we're gonna do is add that all too the list is what we're gonna do. So we're also going to create a variable blissed equal. Okay let me just make it list without the Eye. 1st equals an empty list right here and then we're gonna upend all are things okay? So we're gonna do l dot append. It's not cold, it's hopeless. That depends. Upper um uh Keep doing Alex list. That depends. Lower list of end and list. Got append special. There we go. Okay. No we're just gonna return one SEC. Now we're just going to return list there. Ok now let's test it out, let's see if it works or not. Okay so we're just gonna put over here outside of the function. Print what's the function it is? String type counter. Um And let's give it a random string. We're gonna have some numbers and characters and Yes. Okay just anyway so let's I think Python three and in the name of our oh so And 0 15. 4 and three. There are no uppercase letters so that's correct. A lot of lower case letters I assume that that's correct. There are four. Numbers 1 to 4 and there are three special characters and yep that is true let's try with the with the string. That's a little bit easier to read. Let's have four um numbers 12344 Letters. We're gonna have four special characters. And for uppercase letters just so we can test it out, it should all they should not equal for now and they all equal for. Perfect. So two things that we're just gonna due to wreak to clean up our code a little bit is that we don't need to append this many times. Return all of our variables inside of the list. So we just put the list that the square brackets there and then we just put up er upper comma, lower kama, numb comma special because that's one thing we can do to clean up a little bit and as you can see it's still returns for for before and another thing that we can do is just get rid of the equals true because this is right here is just gonna end up being this is a bully in any way, like this is gonna end up being a bully anyway and if this is true then it's going to add 12 upper. Um I hope I explained that correctly. Um Mhm. No that should still work and it still works perfectly so there we just cleaned up our code a little bit so we didn't have to repeat ourselves too many times and yeah if you guys can think of any other ways to re factor this and let me know or if you came up with your own special solution that would be awesome anyway, thank you guys so much for watching and I'll see you guys, I want to see you guys, I'll teach you guys later.
16 Views 0 Likes 0 Comments

We're given a string and we have to return the amount of uppercase letters, lowercase letters, special characters, and integers in a list

Comment
Leave a comment (supports markdown format)