Start learning free

LESSON 71 OF 287 · STRINGS I

Top K Brands V1 in Python: Building Strings from Variables

Practice Read the full explanation

E-commerce search page: when a shopper searches a product, show the top-rated brand selling it. In v1 there's just one product and one brand.

You start with:

product = "Laptop"
brand = "TechPro"
rating = 4

Build:

Laptop
1. TechPro (4)

Rules:

  • The first line is the product name.
  • The second line is a ranked entry written as "1. Brand (rating)".
  • Keep each line in its own variable (product_line and brand_line), then join them with "\n" into one string called output.

Build brand_line by concatenating "1. ", the brand, the literal " (", str(rating), and ")". A number must go through str() before it can be joined to text.

product_line + "\n" + brand_line joins both lines into one string.

product = "Laptop"
brand = "TechPro"
rating = 4

product_line = product
brand_line = "1. " + brand + " (" + str(rating) + ")"
output = product_line + "\n" + brand_line

STEP 1 OF 3
    Ctrl + Enter to check

    PYTHON · STRINGS I

    Top K Brands V1 in Python, explained

    Problem overview

    This project is an e-commerce search page: when a shopper searches a product, it shows the top-rated brand selling it. Version 1 is the smallest case, one product and one brand, and it is all about building text. The starting variables are product, brand and rating.

    The program must produce a string called output that has two lines: the product name, then a ranked entry written as 1. Brand (rating). With the starting data the string reads:

    Laptop
    1. TechPro (4)

    Nothing is printed in this version. The result lives in output.

    Approach: one variable per line

    Instead of building the whole string in one long expression, the lesson makes each line its own variable, product_line and brand_line, and joins them at the end. Small pieces are easier to read and easier to fix when one of them is wrong.

    The only language feature that matters here is how Python builds strings. The + operator joins two strings. It does not join a string and a number, so rating, which is an int, has to be converted with str() first.

    Step by step

    1. Create product_line. The first line is just the product name, so it is set to product. No other text is added.
    2. Create brand_line. It is assembled from five pieces in order: the rank and its dot with a trailing space, the brand, a space and an opening parenthesis, the rating converted to text, and a closing parenthesis. Pay attention to the spaces: one after 1. and one before (.
    3. Create output. Join product_line, the newline character "\n" and brand_line. The escape \n is a single character that starts a new line wherever it sits in the string.

    The solution in Python

    product = "Laptop"
    brand = "TechPro"
    rating = 4
    
    product_line = product
    brand_line = "1. " + brand + " (" + str(rating) + ")"
    output = product_line + "\n" + brand_line
    

    Some details worth knowing:

    • The literal parts keep their spaces. The pieces are "1. ", brand, " (", str(rating) and ")". Joined, they give 1. TechPro (4). Dropping the space inside " (" would give 1. TechPro(4).
    • Strings are immutable. Each + creates a new string and leaves the old ones unchanged. That is why product_line can be reused later without surprises.
    • Newline inside the string. output holds one string with a line break in the middle. If you ran print(output) it would show two lines, while repr(output) shows 'Laptop\n1. TechPro (4)'.
    • An f-string gives the same line. f"1. {brand} ({rating})" converts the number for you. The lesson uses + and str() so that each piece is visible.

    Edge cases and common mistakes

    • Joining a number without str(). " (" + rating raises TypeError: can only concatenate str (not "int") to str.
    • Typing /n instead of \n. A forward slash is just a character, so the output would contain a literal /n and stay on one line.
    • Hard-coding the answer. Writing "1. TechPro (4)" passes for the starting data but fails as soon as brand or rating changes. Build the line from the variables.
    • Extra spaces or a trailing newline. The expected text has no space at the end of a line and no newline after the last line.

    Testing

    • product = "Phone", brand = "NeoByte", rating = 5 gives Phone then 1. NeoByte (5).
    • rating = 10 works the same way: str(10) is "10", so the entry ends in (10).
    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free