The Man Who Only Says Ends of Words:
- Let’s imagine we know a man that can only say the ends of words. For example, he goes to say good evening, and what comes out is: od ning. We want to predict what he’s going to say.

- If we look at the first word, Good, we can get the last half of the word by concatenating the characters at the indexes [2] and [3], storing this into the variable end1.
python
1word1 = 'Good'
2end1 = word1[2] + word1[3]
3print(end1)Output:
1odSlice Formula and Shortcuts:
- Slice Formula: variable [ start : end + 1]
Using the String Slice Shortcut:
- Leave out the end index and the slice will go to the end of the string. And, we’ll also add the second word, Evening. We can print both of our slices out and we get the result, od ning, as shown below:
python
1word1 = 'Good'
2end1 = word1[2:]
3
4word2 = 'Evening'
5end2 = word2[3:]
6
7print(end1, end2)Output:
1od ningThe Index at the Halfway Point:
- Make the program better for all words, by using the len() function!!
- The len() function will let us calculate the halfway index of our word.
- We can calculate the halfway index of our string by using the length function divided by 2. However, division returns a float (i.e., decimal), which is a problem because only a whole number can be used as an index.
- Therefore, we can use integer division, which is two division signs: //
- // means integer division and also rounds down to the nearest integer
python
1word1 = 'Good'
2half1 = len(word1) // 2
3end1 = word1[half1:]
4
5word2 = 'Evening'
6half2 = len(word2) // 2
7end2 = word2[half2:]
8
9print(end1, end2)- half1 is 4 // 2 = 2
- half2 is 7 // 2 = 3
Output:
python
1od ning![Diagram showing the Python slice formula variable[start:end+1] with labeled start and end positions](/_image?href=https%3A%2F%2Fwkapakosstorage.blob.core.windows.net%2Fcis240%2Fimages%2FproblemSolving-2.jpg&w=1000&h=395&f=webp)