Chapter 4: More on strings and donkeys

Start Here

Chapter 1: In which a cow surprises you 

Chapter 2: The Time traveling cow

Chapter 3: Greedy banker strings

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


Strings, lists and hippies

News of the day: A string is also a list

This may come across as a surprise to you, unless you started programming in the 60s, and look like this:

Photo of scientists

Due to historical reason, a string is also a list. What does that mean? Let’s look at our original list:

This is how you print a list, as we have seen. But we can do the same for a string.

So far, so good.

As you can see, Python treats your string as a list, and allows you to print each character individually. This will come in useful later.

The Moo-Inator writes down the names. “Tomorrow, we will pay a visit to the Crooks R Us Bank, and retrieve the Golden Laptop.”

“Before we pay a visit to that hell hole, I have to call my boss and let him know I won’t be in. He won’t be happy, but this is for the good of humanity, and so I have to do it. I will tell him the truth– A talking cow from the future told me I have to save the world from zombie bankers. I’m sure he will understand.”

“Okay. Honesty is the best policy, I have always printed on the screen,” says Moo-Inator.

Above is an example of using a single quote within a double quote.

You find a pay phone (because you live in the 80s, and mobile phones haven’t been invented yet), and make a call. Your boss seems worried.

“Underpants. Where are you? I am beginning to get worried. We really miss you at the office, you know?”

“Something is wrong,” you say. “He is never this nice.”

In the background, you can hear your pet donkey braying.

The Moo-Inator takes the phone from you. “What’s the donkey’s name?”

“We just call him Donkey.”

find() is used to search within a string. Python is case sensitive, which is why it couldn’t find “donkey”. Try again:

The find() function returns the location where the string you are searching for starts. In this case, it is location 23. Remember we said a string is also an array? That is why we can do sentence[23], and get the letter ‘D’.

The replace function replaces a part of your string with another.

“Wolfie is fine, dear. Just fine. Where are you?”

Moo-Inator put down the phone. “Your boss is already dead.”

Leave a Reply

Your email address will not be published. Required fields are marked *