Start learning free

LESSON 77 OF 287 · STRINGS - CHALLENGES

Burrrrp in Python: Repeating Strings

Practice Read the full explanation

Given a number n, return the sound of a burp with n letters r: "bu", then n letters r, then "p".

 

Example 1:

Input: n = 4
Output: "burrrrp"
Explanation: "bu", then 4 letters r, then "p". That's the burp in this lesson's title.

Example 2:

Input: n = 1
Output: "burp"
Explanation: The shortest burp has a single r.

Example 3:

Input: n = 7
Output: "burrrrrrrp"
Explanation: "bu", then 7 letters r, then "p".

 

Constraints:

  • n is a whole number from 1 to 20.
  • The result is all lowercase.
STEP 1 OF 1
    Ctrl + Enter to check

    PYTHON · STRINGS - CHALLENGES

    Burrrrp in Python, explained

    Problem overview

    The method burrrrp receives a number n from 1 to 20 and returns the sound of a burp with n letters r: "bu", then n times r, then "p", all lower case.

    For n = 4 the result is "burrrrp". For n = 1 it is "burp", the shortest burp. For n = 7 it is "burrrrrrrp".

    Approach

    Only the middle part changes with n. The start, "bu", and the end, "p", are always the same. The middle is one letter repeated, and in Python "r" * n repeats a string n times. Join the three pieces with +.

    You can think of the answer as a template with one blank. The template is bu, a blank, and p, and the blank is filled with exactly n letters r. Python has no special function for this. The repetition operator already does the job, and it works for any non-negative whole number.

    The * operator means repetition when one side is a string and ordinary multiplication when both sides are numbers, so "r" * 4 is "rrrr" while 2 * 4 is 8. Python decides from the types of the two operands.

    Step by step

    1. Start with the fixed beginning, "bu".
    2. Add the repeated letters: "r" * n. Multiplication binds tighter than +, so it is done before the pieces are joined.
    3. Add the fixed ending, "p", and return the result.

    The solution in Python

    class Solution:
        def burrrrp(self, n: int) -> str:
            return "bu" + "r" * n + "p"
    

    Some details worth knowing:

    • Operator precedence matters here. "bu" + "r" * n + "p" means "bu" + ("r" * n) + "p". If you wrote ("bu" + "r") * n + "p", you would repeat "bur" instead.
    • Each + creates a new string, because Python strings are immutable. For three small pieces this is not a concern.
    • The method is named burrrrp, exactly as in the editor, with four r's.

    The burp grows with n

    Only the number of r's changes as n grows:

    nresult
    1"burp"
    2"burrp"
    3"burrrp"
    4"burrrrp"
    5"burrrrrp"

    The length of the result is always n + 3: the three fixed characters b, u and p plus n letters r.

    The same idea with a named variable

    You can build the middle part first and then wrap it. With n = 7 the code below prints rrrrrrr, then burrrrrrrp, then 10 10:

    n = 7
    r = "r" * n
    print(r)
    print("bu" + r + "p")
    print(len("bu" + r + "p"), n + 3)
    

    The last line compares the length of the result with n + 3, and they agree. A length check like that is a quick way to catch a missing or extra letter when your output looks almost right.

    Since the string is built by joining, the order of the pieces decides the order of the letters. Putting "p" first would give a very different sound, so read the title and the examples to see which piece comes where: bu first, the r's in the middle, p last.

    Edge cases and common mistakes

    • The smallest burp. n = 1 gives "burp", with a single r.
    • Counting the r in "bu" or "p". Neither contains an r. The letters between them come only from the repetition.
    • Passing a string for n. "r" * "4" raises a TypeError. The number must be an int.
    • Parenthesising the wrong part, as in ("bu" + "r") * n + "p", which for n = 4 gives "burburburburp".

    Testing

    • n = 4 gives "burrrrp".
    • n = 1 gives "burp".
    • n = 7 gives "burrrrrrrp".
    • n = 20 gives "burrrrrrrrrrrrrrrrrrrrp".
    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free