Lesson 5, Bit 2: String Slices

A segment of a string is called a slice. Selecting a slice is similar to selecting a character:

Code Result
name = 'Monty Python'
name[0:5]
Monty
name[6:12] Python

The operator [n:m] returns the part of the string from the "n-eth" character to the "m-eth" character, including the first but excluding the last.

Launch Exercise

If you omit the first index (before the colon), the slice starts at the beginning of the string. If you omit the second index, the slice goes to the end of the string:

Code Output
fruit = 'banana'

print(fruit[:3])
ban
print(fruit[3:]) ana

Launch Exercise

Launch Exercise

If the first index is greater than or equal to the second the result is an empty string, represented by two quotation marks:

Code Output
fruit = 'banana'

print(fruit[3:3])
(empty string)
print(fruit[4:3]) (empty string)

An empty string contains no characters and has length 0, but other than that, it is the same as any other string.

Just like with negative indices, you can use negative numbers to slice backwards from the end of the string.  A negative number as the first argument

Code Output Notes
sentence = "Tis but a flesh wound!"

print(sentence[-2:])
d!

We start 2 characters in from the end and display the remainder of the string.

print(sentence[-6:-1]) wound

We start 6 characters in from the end of the string and end 1 character from the end of the string.

print(sentence[:-15]) Tis but

We start at the beginning of the string and end 15 characters from the end of the string.

Launch Exercise

You can also combine positive and negative numbers, though it might get confusing. Be careful if you decide to do this:

Code Output Notes
sentence = "Tis but a flesh wound!"

print(sentence[10:-1])
flesh wound

We start on the 10th index (the 11th character) and end one character from the end.

Finally, recall the range and randrange functions.  Remember how you could set a step argument?  Slicing allows you to step as well with an optional third argument.  Note carefully how the step argument works.  You will always get the first character in your set, then skip ahead n-number of characters to get to the next one.

Code Output Notes
sentence = "Tis but a flesh wound!"

print(sentence[::2])
Tsbtafehwud

We start at the beginning and we go to the end and we want every 2nd character.  The first, 3rd, 5th, etc characters are selected.

print(sentence[1::3]) ib fswn

We start with index 1 (the second character) and go to the end, then select every 3rd character.

sentence[-10:-1:4] ewd

We start at the 10th character from the end and go to 1st character from the last character, then take every 4th character.

Launch Exercise

Strings are Immutable

It is tempting to use the [] operator on the left side of an assignment, with the intention of changing a character in a string. For example:

Code Output
greeting = 'Hello, world!'

greeting[0] = 'J'
TypeError: object does not support item assignment

The "object" in this case is the string and the "item" is the character you tried to assign. For now, an object is the same thing as a value, but we will refine that definition later. An item is one of the values in a sequence.

The reason for the error is that strings are immutable, which means you can’t change an existing string. The best you can do is create a new string that is a variation on the original:

Code Output
greeting = 'Hello, world!'

new_greeting = 'J'+greeting[1:]

print(new_greeting)
print(greeting)
Jello, world!
Hello, world!

This example concatenated a new first letter onto a slice of greeting. It has no effect on the original string.

Looping and Counting

The following program counts the number of times the letter a appears in a string:

Code Output
word = 'banana'

count = 0

for letter in word:
    if letter == 'a':
        count += 1

print(count)
3

This program demonstrates another pattern of computation called a counter. The variable count is initialized to 0 and then incremented each time an 'a' is found. When the loop exits, count contains the result—the total number of a's.

The in Operator

The word in is a boolean operator that takes two strings and returns True if the first appears as a substring in the second:

Code Result
'a' in 'banana' True
'seed' in 'banana' False

You can also use the logical operator not to check if something is not found in a string:

Code Result
'a' not in 'banana' False
'seed' not 'banana' True

Here is how you might use this in code:

Code Output
word = 'banana'

if 'a' in word:
    print('We found an A!')
else:
    print('There is no A in this word')
We found an A!
dessert = 'banana pie'

if 'pie' not in dessert:
    print('No pie for you!')
else:
    print('We\'re having pie tonight!')
We're having pie tonight!

Launch Exercise

String Comparison

The comparison operators work on strings.

To see if two strings are equal:

Code
if word == 'banana':
    print('All right, bananas.')

Other comparison operations are useful for putting words in alphabetical order:

Code
if word < 'banana':
    print(word+', comes before banana.')

elif word > 'banana':
    print(word+', comes after banana.')

else:
    print('All right, bananas.')

Python does not handle uppercase and lowercase letters the same way that people do. All the uppercase letters come before all the lowercase letters, so:

Code
Pineapple comes before banana.

A common way to address this problem is to convert strings to a standard format, such as all lowercase, before performing the comparison. Keep that in mind in case you have to defend yourself against a man armed with a Pineapple.

Concatenating Strings

Concatenation is a fancy word for combining.  When we concatenate strings, we are combining them into a single string.  We saw way back in Lesson 1 that the + operator can be used to "add" strings together.  This is concatenation.  You can concatenate strings and variables together:

Code Output
fruit = 'banana'
dessert = 'pie'

print(fruit + dessert)
bananapie
dessert = 'banana ' + 'pie'

print(dessert)
banana pie
fruit = 'banana'

print(fruit+' pie')
banana pie

You can also use the shortcut code += to concatenate multiple strings.

Code Output
fruit = 'b'
fruit += 'a'
fruit += 'n'
fruit += 'a'
fruit += 'n'
fruit += 'a'

print(fruit)
banana
fruit = 'banana'

fruit += ' '

fruit += 'pie

print(fruit)
banana pie

Launch Exercise

Replicating Strings

We also saw way back in Lesson 1 that the * operator can be used to "multiply" strings. 

Code Output
greeting = 'Hi!'

print(greeting * 5)
Hi!Hi!Hi!Hi!Hi!

Launch Exercise