HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

8) Text and Strings Lesson

Python Indices

8 min to complete · By Martin Breuss

Welcome back from error-land! In this lesson, you'll keep moving forward and learn more about working with strings in Python.

You might have found string concatenation to be quite fun, and then it's easy to go overboard and stick just a little too much text together. Luckily, Python also gives you the chance to break strings up into pieces. In this lesson, you'll learn how to pick out single characters from an existing string using string indexing.

However, before you're ready to start disassembling strings, there's the concept of European floor numbering that you need to consider:

Building with windows, photo by https://unsplash.com/@heytowner

If you like the look of these houses, but the concept still sounds confusing and unrelated, read on, and it'll tie together quickly with the necessary prerequisite knowledge for string indexing.

Zero-Based Numbering

In Europe, the stories in houses are numbered starting with the ground floor, or floor #0. In order to get to level #1, you'll have to climb one set of stairs.

Most programming languages take the same approach when numbering items in a collection, and so does Python. Since you already know that strings are collections, it won't be surprising that it applies to strings as well.

Colorful illustration of a light bulb

Note: A string is a collection of characters where each character has a defined index position. The first letter has the index 0, the second letter has the index 1, etc. This is called zero-based numbering in programming.

When you think back to one of the first strings you've encountered in this section, what would the indices of each of the letters be:

"hello"

Zero-Based Number Practice

Give it a try and practice this type of zero-based numbering with some additional strings.

Colorful illustration of a light bulb

Info: Feel free to get out your notebook and write out some examples manually. Using pen and paper can be a great companion to better understand and practice programming concepts.

If you go ahead and break it down, you can look at an indexed string in Python like in the example below:

0 1 2 3 4
---------
h e l l o

Okay, so indexing starts at 0 in Python. Now, how would you be able to access one of these characters in order to do something with it?

Python String Index

Python gives you a handy syntax to pick out only a part of a string through a process that is called indexing. Take a look at how it works:

greeting = "hello"
greeting[0]  # h
greeting[4]  # o

Using the square brackets at the end of a string opens up a window into that string. You can use the brackets like tweezers to pick out the character in that string that you are interested in. There's only one piece of information that you need to provide Python with so that it knows which part of the string you want. That is the index number of the character that you're looking for.

These indices translate directly from how you saw them assigned via zero-based numbering. It means that if you want to pick out the first letter of a word, you can do this with [0]; if you want the second one, you need to type in [1].

Python String Practice

Use the playground below to print a new string that impersonates the Santa Claus impersonator from your local shopping mall by saying "hohoho". You can only use the provided string! Pick out the relevant characters and concatenate them in order to print out the new word.

# Use the string indices to print "hohoho" from the santa variable
santa = "hello"

Great job if you got this to work, and ho-ho-ho to you! You might have struggled with needing to count to the end of your string in order to pick out the last letter, o. To make locating characters at the end of a word easier, Python also allows you to index starting from the back.

Accessing Characters Backwards

If you need to pick out the last few characters from a long string, for example, the file extension from a file name, you might soon want to sigh in desperation! Do you really need to count all the way to the end every time?! You don't! Python's got you covered!

Additionally to indexing strings from the beginning, starting with 0, each string is also indexed from the end, starting with -1:

-5 -4 -3 -2 -1
--------------
 h  e  l  l  o

The rest of the indexing syntax stays the same, which means that you can access the "o" in this word also with:

"hello"[-1]

This way of negative indexing makes some string operations much easier. For example, fire up your Python REPL and fetch just the file extension from "wow-that-is-such-a-long-filename.txt" using negative indexing.

Summary: Python Indices

  • Indices: An index is the location of a character in a string
  • Each character has a defined index that is denoted with an integer
  • Zero-Based Indexing: Python starts counting at 0
  • Indexing Syntax: A pair of square brackets wrapping the index of the character that you want to access
  • Positive And Negative Indexing: You can index from the beginning, starting at 0, or backward from the end, starting at -1