String Manipulation

Quiz: counts toward finishing the lesson. Check: just for practice.

in, not in

There are operators called in and not in that can be used to check whether a string contains another string. For example:

len

There is a function called len that returns the length of a string:

upper and lower

You can use the functions upper and lower to get an upper-case or lower-case version of a string.

Operators vs. Functions vs. Methods

Notice the different syntax we've used for in and not in, len, and upper and lower. The in and not in operators are used between two values (the operands) in a format similar to arithmetic operators, like * and /. These arithmetic operators each have two numeric operands. However in the examples for in and not in, we used strings for both operands.

The len function is called in the same way we've called other functions. We first give the name of the function, and then list inside parentheses any values we're passing to the function. In this case we're passing a string to the function.

The upper and lower methods are called using the dot notation syntax that we use with objects. That's because strings (like everything else in Python) are objects that have certain methods defined for them. In this case, we don't have to pass the string as an argument. You can think of it as politely asking the string if it would be so kind as to give you an upper-case (or lower-case) version of itself. (The string is implicitly passed to the method as the self argument.)

Indexing

We can get the character at a certain position, or index, in a string by indexing into the string. Imagine numbering the letters of a string in order, starting with zero. So in the string "onyx", the "o" is at index zero, the "n" is at index 1, the "y" is at index 2, and the "x" is at index 3. Notice that the index of the last character is the length of the string minus 1. We index into a string by following it with square brackets containing the index that we want to access.

We can also use negative indices, in which case we count from the back of the string. The character at index -1 would be the last character in the string, the one at -2 would be the next-to-last character, and so on.

Check your understanding. Predict the result. Count from the back of the string.

word = "bookshelf"
x = word[-3]
y = len(word)

Predict what each variable holds after the program finishes. The program is not run for you. Change the values below to your predictions, and write strings with quotes:

  • final_x is the value of x at the end
  • final_y is the value of y at the end
Hint 1

Negative indices start at -1 for the last character. len counts every character.

Slicing

Slicing a string gives you back part of that string, based on the indices for the start and end of the part you want.

Notice that the substring you get back starts at the first index, and goes up to, but does not include the second index (similar to making a range). If you omit the first index, the substring starts at the first character. If you omit the second index, the substring ends at the last character.

The character at index 6 (the seventh character) doesn't get included in the first slice, but does in the second slice. We can also add a third number after a second colon, the "stride". A stride of 2 gives every other character, a stride of 3 gives every third character, and so on. A stride of -1 walks the string backward.

Check your understanding. Predict the result. Look at where each slice starts and which way it moves.

text = "abcdefgh"
x = text[1:6:2]
y = text[::-2]

Predict what each variable holds after the program finishes. The program is not run for you. Change the values below to your predictions, and write strings with quotes:

  • final_x is the value of x at the end
  • final_y is the value of y at the end
Hint 1

The stride is the step between characters. A negative stride walks backward from the last character.

Comparison Operators

We can use the normal comparison operators with strings, where A < B is true when A comes before B in lexicographic (dictionary) order.

However, there is a catch. Characters are stored as numbers, and upper-case letters have lower numbers than lower-case letters, so Python puts every upper-case letter before every lower-case letter.

One way to handle this is to convert the strings to upper-case (or lower-case) before comparing them.

Quizzes

  1. Write a function called last_char that takes a string parameter and returns the last letter of that string.
  1. Write a function called midstring that takes a string parameter and returns a copy of that string minus its first and last letters. If the string passed in has only 1 or 2 letters, the function should return the empty string "".
  1. Write a function called sort_two_strings that takes two string parameters and returns a single string of both of them in dictionary order, ignoring case. For example, if the strings "aardvark" and "Zebra" are passed, it should return "aardvark Zebra".
  1. A palindrome is a string that reads the same forward or backward. Write a function called is_palindrome that takes a string parameter and returns True if that string is a palindrome and False otherwise.