Udacity CS 101: Strings and its Operations Part I

Like we mentioned earlier, strings are actually sequence of characters, hence when you give an instruction as such:

hair = "blue"

Katy Perry's Smurf Blue Hair

‘Cos Blue is Cool
Image Courtesy of http://newfashionblog.net

Python actually remembers that hair is the variable name for a string, and the contents of the string are the characters ‘b‘, ‘l‘, ‘u‘, ‘e‘, in that order. As such it is possible to refer to a particular character in a string by its position. If I wish to print out the letter ‘l’ in the word blue, I would:

print hair[1]

# and Python will print out


Now at this moment you might be scratching your head and thinking to yourself, isn’t ‘l‘ the second character in the sequence? Don’t worry its a perfectly legitimate thought. Now computers start counting at 0, most of the time, so we say that the letter ‘h‘ is at position 0 of the word hair. In this manner:

print hair[0] # gives you h

print hair[3] # gives you r

print hair[4] # gives you an error because you are trying to refer to a letter that does not exist

And what you have a really long string, something like theColourOfMyHair or howManyTimesHumansBlinkInALifetime,

Blink 182

No no no, not this Blink!
Image courtesy of http://www.sweetslyrics.com

how are you going to get the last letter? Are you going to count? Fortunately for you the creators of Python have come up with a system of counting backwards,

hair = "theColourOfMyHair"

print hair[-1] # will give you 'r'

And that is not all. Not only can you select a single letter, you can select a sequence of characters within a string (which itself is a sequence of characters! String-ception!). We call that sequence a sub-string. Here’s how to do it:

hair = "theColourOfMyHair"

print hair[3:9] # will give you 'Colour'


Your hair can be many colours, which one is it?
Image courtesy of http://mynokiablog.com

You can tell Python which sequence you want by specifying the start position, followed by a colon ‘:’, and the end position. The start position is inclusive but the end position is not. Due to this phenomenon, you can easily figure out the length of the string that you have extracted, (end position – start position) .

Wait there’s more! What if you want to print out ColourOfMyHair, you still have to count the index of the last digit right? There’s where these nifty tricks come in:

hair = "theColourOfMyHair"
print hair[3:] # will give you ColourOfMyHair
print hair[:8] # will give you theColour

Interesting right! Head on to the next quiz!

Just a note on the Quiz titled “Understanding Selection”. It’s a really tricky test so its best to try to give s an actual value, for example think of s as the string “UDACITY”, and write out what each examples of selection will give you. Try to really understand the concept because basically when building your search engine, it’s about recognizing links, and you’ll definitely need to be able to select sub-strings well to build a search engine that works! All the best!

This Udacity post is based on Udacity CS 101 Unit 1 Chapters 26, 27, 28, 29, 30 and 31

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s