Chapter 3: Greedy banker strings

Start Here

Chapter 1: In which a cow surprises you

Chapter 2: The Time traveling cow

Chapter 3: Greedy banker strings (This chapter)

Chapter 4: More on strings and donkeys

Chapter 5: Crooks R Us Bank & Lists

Chapter 6: Orc functions

Chapter 7: Dictionaries, Locks, and Gobbledygook

Chapter 8: Python vs the Zombies

Chapter 9: The Doggy-Nator and List Comprehensions

Chapter 10: In which you Eval the Doggy


Greedy Pigs use strings

After you’ve had some satisfying gourmet burgers, you ask the Moo-Inator how you can save the future.

“We need to find all the greedy bankers in the country. Do you know them?”

You look at the cow like its gone crazy. “You jokin’ right?”

But the cow is serious. “Surely there can be no more than a dozen greedy bankers in the country? Banking is an honest profession in your time, right?”

You shake your head and wonder what century the cow is from. “There are ten thousand greedy bankers in this city alone.”

“Tell me the names of a few of them.”

Right folks. Coding time. You need to take five names from the user. Because a talking cow from the future told you. Seriously, when did you last visit your psychiatrist?

So, let me show you how not to do it:

That’s a bit tiring, isn’t it? Try this:

nameList is now an empty list. We will store our names in it.

Again, remember to ident the code below the for loop. Let’s go over the lines one by one. Feel free to skip if you have done programming before, and know what a for loop is.

The for loop allows us to run the same code multiple times. We have already seen the range() function, which generates a range of numbers from 0 to 4 (hence five numbers). The for loop says we need to run the code below 5 times. The ‘i’ is just a variable. You can call it anything. LoopCounter, Pizzas, or CowDung. Don’t believe me?

The actual variable can be used, as we will see later. Coming back to our code, the second line :

We already know what this is. We are reading the name from the user, and storing it in a variable called name.

Remember the list called nameList we created? It was empty to start with. We now add the name we just read to it. Once you are done with the names, you can print them out:

Of course, that isn’t the proper way to print names. It only works because we are in a debug shell. If this was live code, this might not work. The proper way to print the names is:

Code explanation:

name is a variable we are using. Again, we could call it anything. This loop says, for each name stored in nameList,

Print that name.

Your turn. Type this code in, and enter five names. Just make sure the third name is “1Banker1”, and the last name is in ALL CAPS.

There are many ways to work with our list. I have shown you one way (using a for loop) above. But you can also access each element. Our list contains five names. As I said earlier, in Python, the numbers start from zero. So the first name is at position zero in the list. What does that mean? Type this:

So if we want to see the second name, we have to type

Let’s look at the last name now.

This is in all caps, which is considered shouting. Well, why don’t we fix it? Python gives us several inbuilt tools to work with strings.

Side Note: What’s a string?

In Python, string is a type of a variable, usually used to store names, addresses etc. Anything declared with quotes, ” or ‘, becomes a string.

In the last example, Python is complaining because we have not enclosed the sentence within quotes (” “), so Python thinks its an instruction, and tries to execute it.

String is the most popular type in Python, and used a lot.

End of sidenote

Coming back to our example, we could manually fix our all caps, but why should we when Python will do that for us? There are a large number of inbuilt functions for strings, and we will use them instead:

The lower() function converts a string to lower case. We can call it on any string.

But lower case isn’t good enough for us. Most names have the first word capitalised. I wonder if that is possible?

Now we got it. The title() function converts string to the proper format for names, so the first letter is capital. How do we update the array? Simples.

We still have a problem. Our middle name has extra ‘1’s in it, that we typed in by mistake. Luckily, we can remove it easily:

The strip() function is actually used to remove whitespace. Imagine you read a name from a file:

There is a lot of space of the beginning and end of the name, which we don’t want to store. Strip() will remove the space for us. By default, the strip() function removes empty space:

The only problem is that strip() only works at the start and end of the string. So if you had “ban1ker”, it would not work. There are other ways around that, which we will see later.

Leave a Reply