Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: Indexing and Finding in Strings

check

Getting string characters by-position and finding positions of characters.


String Characters and Positions

You can think of strings as an ordered collection of characters. A "character" is a letter, number, space, or anything else you can type with a keyboard.

Finding the Number of Characters in a String

All strings have a defined number of characters. The number of characters in a string is called its "length". In order to find out how many characters a string has, Python provides a build-in function called len():

print(len("12345"))

> 5

A string with no characters is called an "empty string" and has a length equal to 0. An empty string can be created by using two quotes with nothing in-between:

print("")

> ""

print(len(""))

> 0

Practice: Finding a String's Length

Using the len() function noted above, complete the following steps:

  1. Create a variable named keyboard_row, and assign it the string "qwertyuiop[]".

  2. Create a variable named keyboard_row_length, and using the function and variable above, assign it the length of the string in keyboard_row.

  3. Print the value contained in the keyboard_row_length variable.

    HINT*: Click here for a hint
    *Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
    > 12
    SOLUTION*: Click here for an example solution
    *Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
    keyboard_row = "qwertyuiop[]"
    keyboard_row_length = len(keyboard_row)
    print(keyboard_row_length)
    

Getting the Character at a Specified Position within a String

You can get a character at a given position (also known as an "index") within a string by placing a set of square brackets ([]) immediately following the string (or variable that contains a string). Inside the square brackets, specify the position of the character as an integer:

print("Elbow grease"[3])

> "o"

Indexes (positions) start from zero (0), then count up by one for each character following the first. If a string has a length of 5, then its characters are stored in indexes 0, 1, 2, 3, and 4:

print("Elbow grease"[0])
print("Elbow grease"[1])
print("Elbow grease"[2])

> 

"E" "l" "b"

Positions can also be specified from the back of a string by using negative integers, starting with -1:

print("Elbow grease"[-1])
print("Elbow grease"[-2])
print("Elbow grease"[-3])

> 

"e" "s" "a"

Practice: Getting the Character at a Specific Position

Based on the above information, complete the following steps:

  1. Create a variable named statue, and assign it the string value "Grey gargoyle".

  2. Using the variable above and bracket notation, print the character at index 4.

  3. Using the variable above and bracket notation, print the second character from the front.

  4. Using the variable above and bracket notation, print the third character from the back.

    HINT*: Click here for a hint
    *Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
    > " "
    > "r"
    > "y"
    SOLUTION*: Click here for an example solution
    *Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
    statue = "Grey gargoyle"
    print(statue[4])
    print(statue[1])
    print(statue[-3])
    

Finding a Character's Position within a String

You can find a character's position within a string by using its find method:

print("Elbow grease".find("w"))

> 4

If the character isn't in the string, the find method will return -1:

print("Elbow grease".find("7"))

> -1

Strings are case-sensitive, so uppercase and lowercase letters are treated as different characters. Consequently, the find method will only find a letter's position if its case matches:

print("Bob".find("b"))

> 2

If the same character shows up multiple times, the find method will return the position of the first occurence:

print("banana".find("a"))

> 1

If you wish to find the last occurence of a character, you can use the rfind method instead:

print("banana".rfind("a"))

> 5

Practice: Finding First or Last Position of a Character

  1. Create a variable named car, and assign it the string value "Hyundai Sonata".

  2. Using the variable and the correct method above, print the index position of the letter "i".

  3. Using the variable and the correct method above, print the index position of the first "a".

  4. Using the variable and the correct method above, print the index position of the last "a".

  5. Using the variable and the correct method above, print the length of the first word.

    HINT*: Click here for a hint
    *Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
    > 6
    > 5
    > 13
    > 7
    SOLUTION*: Click here for an example solution
    *Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
    car = "Hyundai Sonata"
    print(car.find("i"))
    print(car.find("a"))
    print(car.rfind("a"))
    print(car.find(" "))
    

When you have completed the above, you are ready to move on to the next exercise!