Learn Python Programming - 26 - Length of a String (Exercise)

Learning to Code: A Journey Through Rock-Paper-Scissors and Beyond

As we begin our journey through the world of coding, it's essential to start with the basics. In this article, we'll explore one of those basic concepts - rock-paper-scissors - and see how we can use it to build upon ourselves. We'll start by creating a simple function that takes a string as input and outputs the number of characters in that string.

To create our function, we first need to understand what we want to do. We want to count the characters in the string and output the result. Sounds simple enough, right? Well, it is! Let's start with the basics. If we see an "e" in our string, we'll go three spaces. So, if we see another "e", we'll go five spaces. And if we're wrong and run out of characters, we want to just output five.

This concept can be applied to any character in the string. We can use a loop to iterate through each character and perform this calculation. But how do we implement it? Well, we need to define our function first. In Python, we can use the following code:

```python

def print_letter(letter):

if letter == "e":

print(" ", end="")

elif letter == "l":

for _ in range(3):

print(" ", end="")

elif letter == "o":

for _ in range(5):

print(" ", end="")

print_letter('h')

```

As you can see, this function takes a single character as input and prints the corresponding spaces. But we want to count the characters in the entire string, not just individual characters.

To achieve this, we need to iterate through each character in the string and apply our calculation. We can do this using a for loop:

```python

def print_and_count_hello():

s = "hello"

count = 0

for letter in s:

if letter == 'h':

count += 1

elif letter == 'e':

count += 3

elif letter == 'l':

count += 1

count += 1

elif letter == 'o':

count += 5

print(letter, end="")

print(count)

print_and_count_hello()

```

As you can see, this function takes a string as input and prints the characters in that string along with their corresponding counts. But we want to return just the final count.

To achieve this, we need to move our `count` variable outside of the for loop:

```python

def print_and_count_hello():

s = "hello"

count = 0

for letter in s:

if letter == 'h':

count += 1

elif letter == 'e':

count += 3

elif letter == 'l':

count += 1

count += 1

elif letter == 'o':

count += 5

print(letter, end="")

return count

print(print_and_count_hello())

```

Now we have a function that takes a string as input and returns the final count.

But what if we want to use this function for something else? What if we want to create our own text messaging app using Twilio API? Well, we can't do that yet. But we're getting there! We've covered some essential concepts like loops, strings, and variables. And we've even started to build a rock-paper-scissors game.

In the next article, we'll dive deeper into building our own rock-paper-scissors game using Python. We'll cover topics like user input, conditional statements, and more. And who knows? Maybe one day we'll create our own text messaging app using Twilio API. Until then, keep coding!

"WEBVTTKind: captionsLanguage: enas what's up is Kazi from clever programmer calm where you learn to code smarter in this video what I want to talk about is string length function I don't know why I did that actually but essentially how this function works is that if I give you something I know it's going on land today if I give you something a string and you tell me how many characters are in there okay so by now hopefully you should kind of know the format of how these problems were laid out and how you can solve them and you can also look at the tests like the cert test that I have to kind of cheat right like oh this is aamani inputs it takes in and yadda yadda okay so try this problem on your own I have the notes below in the YouTube video I have the exercise solution below and if you want to you can go to my website clever programmer calm and enroll in the course so that it can keep track of everything and then you know it's all nice and all in one place anyways let's keep going so I'm going to write the solution right now so spoiler alert close your eyes kids if you haven't tried it do not look at this you don't deserve to look at the solution if you haven't tried it on your own already okay go try it on your own let's get let's get to the solution now so first thing I'm going to do is de F because it's a function then string length because that's what I told you the function name should be right right a function that takes in a string and returns its length right so the function is called string length right inputs how many inputs is the take that takes in a string so the word o should give you a hint that it's only one thing you can say string you can say my string you can call it whatever you want okay I'm just going to call it my string to let you guys know that there's no special meaning behind the word string itself so you don't need to like know what it does what this function requires you to know is how to do a for loop okay if you can do a for loop you can solve problem very easily so I have my string now what do I want to do I'm going to run through that string and count each letter right so if it's hello I want to count that I want to keep track of every letter that I see so when I see an H I go I hold my finger up like this like one when I see an e I go to when I see an L I go three I see another L I go for when I see an O I go five and if I'm wrong if I've run out of characters I want to just output five okay so it sounds like something that I want to run multiple times sounds like a loop four letter in my string right if I do print letter here you guys can see on the right hand side that is going to go through that and is going to print the letter I have to call the function of course string length and let's give it hello and let's run it and on the right hand side you can see that it goes h-e-l-l-o right that's great so we have access to each character now what do I want to do I want to have a tracker variable like count okay and I'm going to set it to zero and every time I see something I will not incremented by one so count is equal to count plus 1 or count plus equal one because such a common operation so we just increment by saying plus equals okay that's how you're going to see it written down in Python pretty much everywhere and if you're programming in some other language like C or C++ you're going to see it written like this all right so I have count equals count plus one so I'm incrementing count by one right increment count by one and how many times do I want to print out count every single time or just at the end if you think of that question in English you can answer that very easily I only wanted to return the count once so should it be part of the for loop or should it be outside for loop it should be outside of the for loop good okay and I just want to hit enter so it looks nice and and yeah now let's try this let's do print and my function right let's see if it prints five okay it prints hello and then it prints out five we don't want to print out hello anymore because we know it works just fine and you get five how does it work if you really break it down well to my function I pass hello right so this part of my string becomes hello then this becomes hello the first time we're going through the loop this is an H right then we just count it by one then the next time we go through the loop this is just an E and then we count it by one then next time we go through the loop this is an L then we increment by one next time we go through the loop it's elegant and we increment count by one and then last time weren't o we increment count by one now count is five and then we return five now if you notice do we do we use the variable letter anywhere else no we don't really use it so you can say whatever you want here or you can just put an underscore here because we don't really use it anywhere so we don't care what that variable is but sure we'll just we'll just leave it as letter at the end of the day I wanted to return count now let's test our function if it prints out your code is correct then our code is indeed correct and we are done let's check it out hopes I can't hard code a value here I have to change this to my string and let's run it and it says your code is correct now guys we're learning all of this to build something big okay we're going to build our own rock-paper-scissors and I don't mean like code Academy calm let me hold your hand and here's the entire code and you feel like you dated but you didn't really learn anything I mean you're going to be doing practically the whole thing I'm going to you the barebone pieces but you're going to be writing your own code and I really want you to be able to do this completely on your own so it's really important that you do all of these exercises and try your best to do them on your own because you will need all of these pieces to then go and try to build your own game so we're going to start off with a simple game like rock-paper-scissors but then after that we're going to do something with Twilio API which is the text messaging at you can build pretty much your own text messaging app okay and you can actually send people text messages on their phones so we're going to be doing some really big stuff here all right no more baby stuff but I want you to be prepared for that that's why I have this for you guys I have notes that solution notes and everything so take advantage of that use everything at your disposal if you haven't already subscribed to this channel and do not miss the next video because in those all of those videos that are coming up we're going to be covering covering really important things I'll see you thereas what's up is Kazi from clever programmer calm where you learn to code smarter in this video what I want to talk about is string length function I don't know why I did that actually but essentially how this function works is that if I give you something I know it's going on land today if I give you something a string and you tell me how many characters are in there okay so by now hopefully you should kind of know the format of how these problems were laid out and how you can solve them and you can also look at the tests like the cert test that I have to kind of cheat right like oh this is aamani inputs it takes in and yadda yadda okay so try this problem on your own I have the notes below in the YouTube video I have the exercise solution below and if you want to you can go to my website clever programmer calm and enroll in the course so that it can keep track of everything and then you know it's all nice and all in one place anyways let's keep going so I'm going to write the solution right now so spoiler alert close your eyes kids if you haven't tried it do not look at this you don't deserve to look at the solution if you haven't tried it on your own already okay go try it on your own let's get let's get to the solution now so first thing I'm going to do is de F because it's a function then string length because that's what I told you the function name should be right right a function that takes in a string and returns its length right so the function is called string length right inputs how many inputs is the take that takes in a string so the word o should give you a hint that it's only one thing you can say string you can say my string you can call it whatever you want okay I'm just going to call it my string to let you guys know that there's no special meaning behind the word string itself so you don't need to like know what it does what this function requires you to know is how to do a for loop okay if you can do a for loop you can solve problem very easily so I have my string now what do I want to do I'm going to run through that string and count each letter right so if it's hello I want to count that I want to keep track of every letter that I see so when I see an H I go I hold my finger up like this like one when I see an e I go to when I see an L I go three I see another L I go for when I see an O I go five and if I'm wrong if I've run out of characters I want to just output five okay so it sounds like something that I want to run multiple times sounds like a loop four letter in my string right if I do print letter here you guys can see on the right hand side that is going to go through that and is going to print the letter I have to call the function of course string length and let's give it hello and let's run it and on the right hand side you can see that it goes h-e-l-l-o right that's great so we have access to each character now what do I want to do I want to have a tracker variable like count okay and I'm going to set it to zero and every time I see something I will not incremented by one so count is equal to count plus 1 or count plus equal one because such a common operation so we just increment by saying plus equals okay that's how you're going to see it written down in Python pretty much everywhere and if you're programming in some other language like C or C++ you're going to see it written like this all right so I have count equals count plus one so I'm incrementing count by one right increment count by one and how many times do I want to print out count every single time or just at the end if you think of that question in English you can answer that very easily I only wanted to return the count once so should it be part of the for loop or should it be outside for loop it should be outside of the for loop good okay and I just want to hit enter so it looks nice and and yeah now let's try this let's do print and my function right let's see if it prints five okay it prints hello and then it prints out five we don't want to print out hello anymore because we know it works just fine and you get five how does it work if you really break it down well to my function I pass hello right so this part of my string becomes hello then this becomes hello the first time we're going through the loop this is an H right then we just count it by one then the next time we go through the loop this is just an E and then we count it by one then next time we go through the loop this is an L then we increment by one next time we go through the loop it's elegant and we increment count by one and then last time weren't o we increment count by one now count is five and then we return five now if you notice do we do we use the variable letter anywhere else no we don't really use it so you can say whatever you want here or you can just put an underscore here because we don't really use it anywhere so we don't care what that variable is but sure we'll just we'll just leave it as letter at the end of the day I wanted to return count now let's test our function if it prints out your code is correct then our code is indeed correct and we are done let's check it out hopes I can't hard code a value here I have to change this to my string and let's run it and it says your code is correct now guys we're learning all of this to build something big okay we're going to build our own rock-paper-scissors and I don't mean like code Academy calm let me hold your hand and here's the entire code and you feel like you dated but you didn't really learn anything I mean you're going to be doing practically the whole thing I'm going to you the barebone pieces but you're going to be writing your own code and I really want you to be able to do this completely on your own so it's really important that you do all of these exercises and try your best to do them on your own because you will need all of these pieces to then go and try to build your own game so we're going to start off with a simple game like rock-paper-scissors but then after that we're going to do something with Twilio API which is the text messaging at you can build pretty much your own text messaging app okay and you can actually send people text messages on their phones so we're going to be doing some really big stuff here all right no more baby stuff but I want you to be prepared for that that's why I have this for you guys I have notes that solution notes and everything so take advantage of that use everything at your disposal if you haven't already subscribed to this channel and do not miss the next video because in those all of those videos that are coming up we're going to be covering covering really important things I'll see you there\n"