Start learning free

LESSON 76 OF 673 · STRINGS - CHALLENGES

Repeat Last Char in Python: Indexing and String Repetition

Practice Read the full explanation

Given a string s and a number n, return a new string made of the last character of s, written n times in a row.

 

Example 1:

Input: s = "banana", n = 3
Output: "aaa"
Explanation: The last character of "banana" is "a". Written 3 times, it gives "aaa".

Example 2:

Input: s = "Wow!", n = 2
Output: "!!"
Explanation: The last character can be any character: here it is the exclamation mark.

Example 3:

Input: s = "z", n = 1
Output: "z"
Explanation: A string of one character: its last character is "z", written once.

 

Constraints:

  • s has at least one character and doesn't start or end with a space.
  • s contains English letters, digits, spaces and punctuation.
  • n is a whole number from 1 to 10.
STEP 1 OF 1
    Ctrl + Enter to check

    PYTHON · STRINGS - CHALLENGES

    Repeat Last Char in Python, explained

    Problem overview

    The method repeatLastChar receives a string s and a whole number n from 1 to 10. It returns the last character of s written n times in a row.

    For s = "banana" and n = 3, the last character is a, so the result is "aaa". For "Wow!" and n = 2, the last character is the exclamation mark, so the result is "!!". For "z" and n = 1, the result is "z".

    Approach

    Two small tools solve this. A negative index counts from the end of a string, so s[-1] is the last character whatever the length. And multiplying a string by an integer repeats it: "ab" * 3 is "ababab". Combined, s[-1] * n is the whole answer.

    The two tools are independent. The index decides which character you repeat, and the number decides how many times. Because n is only a count, the same code works for n = 1, where the result is the character itself, and for n = 10, where it is ten copies in a row.

    Step by step

    1. Get the last character with s[-1]. It is a string of length 1.
    2. Repeat it by multiplying with n.
    3. Return the result.

    The solution in Python

    class Solution:
        def repeatLastChar(self, s: str, n: int) -> str:
            return s[-1] * n
    

    Some details worth knowing:

    • The * operator works between a string and an integer, in either order: 3 * "a" and "a" * 3 are the same. The repeated copies are joined into one new string.
    • Multiplying by 0 gives an empty string. The constraints keep n at 1 or more, so the lesson never asks for that.
    • The character can be anything: a letter, a digit, a space inside the string, or punctuation.

    Repeating strings

    String repetition is new here, so a few results are worth seeing. "ab" * 3 is "ababab", and 3 * "ab" gives the same text. The whole string is repeated, which is why the order of the two operations matters: "banana"[-1] * 3 is "aaa", but "banana" * 3 is "bananabananabanana". Pick the character first, then repeat it. Multiplying by a string instead of an integer is an error: "r" * "4" raises TypeError: can't multiply sequence by non-int of type 'str'.

    The same idea with named variables

    Splitting the work into two lines shows both tools separately. For s = "Wow!" the code below prints ! and then !!:

    s = "Wow!"
    ch = s[-1]
    print(ch)
    print(ch * 2)
    

    The first line picks the character, and the second repeats it. When a result looks wrong, printing the intermediate value tells you which of the two steps went astray: the character you picked, or the number of repeats.

    The last character can be any kind of character, such as the ! in "Wow!". The code never looks at what kind of character it is. It only picks the one at index -1 and repeats it, so letters, digits and punctuation are all treated the same way.

    Edge cases and common mistakes

    • A one-character string. s[-1] and s[0] are the same character, and the code still works.
    • Using s[len(s)]. That index is one past the end and raises IndexError. Use -1.
    • Using the first character, s[0], instead of the last. For "banana" that would give "bbb".
    • Multiplying the whole string. s * n repeats all of s, so "banana" * 3 is "bananabananabanana". Index first, then multiply.

    Testing

    • "banana", 3 gives "aaa".
    • "Wow!", 2 gives "!!".
    • "z", 1 gives "z".
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free