Python Python Collections Slices Slice Functions

Phillip Bailey
Phillip Bailey
1,216 Points

Return first 4 words from any iterable

def first_4(word): print(word[:4]) return word

first_4("apple")

If you run this on the string "apple" it returns the "appl" so I'm confused why its not correct. Also the return statement at the end if left alone returns a blank program and seems if nothing was ran. I did call the function as well.

slices.py
def first_4(word):
    print(word[:4])
    return word

2 Answers

Cheo R
Cheo R
35,541 Points

You're on the right track.The way you have it now is that your function will print the first 4 letter in the word, and returns the word itself. What you want to do is return the first 4 letters in the word. So just combine them to return the first 4 letters.

Moderator Edit: moved response to Answer section

Phillip Bailey
Phillip Bailey
1,216 Points

Thank you for that, I see what I did wrong there now :) I am just stuck now on step 3/4 returning the odd values. I'm very close though.

Cheo R
Cheo R
35,541 Points

Key part is:

  • odd index*

If you remember, lists/strings in python are 0-based. So the indexes of a list of length 4 would be 0, 1, 2, 3. You want to return the values when the index is an odd number (i.e. value of index 1, 3).