Start learning free

LESSON 75 OF 673 · STRINGS - CHALLENGES

Stuttering Function in Python: Slice and Concatenate

Practice Read the full explanation

Given a word word, return it said with a stutter: its first two characters, then a hyphen -, then the whole word.

 

Example 1:

Input: word = "dog"
Output: "do-dog"
Explanation: The first two characters are "do". After them come a hyphen and the whole word, "dog".

Example 2:

Input: word = "Hello"
Output: "He-Hello"
Explanation: The characters keep their case: the stutter starts with an upper-case H, as the word does.

Example 3:

Input: word = "ab"
Output: "ab-ab"
Explanation: A word with only two letters: its first two characters are the whole word.

 

Constraints:

  • word has at least 2 letters.
  • word is made of English letters only, lowercase or uppercase: no spaces, digits or punctuation.
STEP 1 OF 1
    Ctrl + Enter to check

    PYTHON · STRINGS - CHALLENGES

    Stuttering Function in Python, explained

    Problem overview

    The method stutterize receives a word of at least two letters and returns it said with a stutter: its first two characters, a hyphen, then the whole word. Letters keep their case.

    For "dog" the first two characters are do, so the result is "do-dog". For "Hello" it is "He-Hello", with the upper-case H kept. For the two-letter word "ab", the first two characters are the whole word, so the result is "ab-ab".

    Approach

    The result has three pieces in a row: a slice of the word, the fixed text "-", and the word itself. The + operator joins strings, so the answer is those three pieces added together.

    Notice that the word appears twice in the answer, once cut short and once whole. You don't need a loop or a helper for that. Python evaluates the slice, then the hyphen, then the word, and joins them left to right into one string. If the result looks wrong, check each piece on its own: print the slice, then print the whole expression.

    Step by step

    1. Slice the first two characters. word[:2] leaves out the start, so it begins at index 0, and it stops before index 2. That keeps indexes 0 and 1.
    2. Add the hyphen as the string "-".
    3. Add the whole word and return the result.

    The solution in Python

    class Solution:
        def stutterize(self, word: str) -> str:
            return word[:2] + "-" + word
    

    Some details worth knowing:

    • A slice stops before its end index, so word[:2] has two characters, not three.
    • Strings are immutable, so each + builds a new string. The original word is never changed, which is why it can appear twice in the result.
    • Because the word has at least two letters, word[:2] always has exactly two characters. A shorter string would give a shorter slice rather than an error: "a"[:2] is "a".

    Seeing the three pieces

    Take word = "dog". The result is built from these parts, left to right:

    1. word[:2] gives "do".
    2. "-" adds the hyphen.
    3. word adds "dog".

    Added together they make "do-dog". Changing the slice changes only the first piece: word[:1] would give "d-dog", and word[:3] would give "dog-dog". That is why the end index of the slice has to be exactly the number of characters the stutter repeats.

    The same idea with a named variable

    Naming the stutter makes the structure visible. For word = "Hello" this prints He, then He-Hello, then Hello:

    word = "Hello"
    start = word[:2]
    print(start)
    print(start + "-" + word)
    print(word)
    

    The last line confirms that word is untouched. The one-line solution does the same work without the variable, and either form passes the lesson's tests.

    Edge cases and common mistakes

    • Two-letter words. "ab" gives "ab-ab": the slice and the word are the same text.
    • Using word[:3] or word[:1]. The first gives "dog-dog" for "dog", the second "d-dog". Count the end index as the number of characters you keep.
    • Using word[2]. A single index returns one character, the third one, not a two-character piece.
    • Changing case. The stutter keeps the word's case, so "Hello" starts with He, not he.
    • Forgetting the hyphen, or typing a space or an underscore instead.

    Testing

    • "dog" gives "do-dog".
    • "Hello" gives "He-Hello".
    • "ab" gives "ab-ab".
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free