Learn with Yasir

Share Your Feedback

Find and Fix Mistakes in Python Strings – Practice with Common String Errors

Improve your Python skills by identifying and correcting common string mistakes. Practice string operations, formatting, and debugging techniques with hands-on examples.

Topic: strings


🧪 Fix & Find Questions

🟢 Beginner Fix & Find

  1. Fix the incorrect string concatenation:

    name = "Alice"
    age = 25
    message = "Hello " + name + ", you are " + age + " years old."
    print(message)
    

    Hint: 💡 You can't concatenate strings with integers directly in Python. Consider using string formatting.

    🔍 View Issue & Fixed Solution

    Issue: Trying to concatenate strings with integers using the + operator causes a TypeError.

    ✅ Fixed Solution

    name = "Alice"
    age = 25
    message = "Hello " + name + ", you are " + str(age) + " years old."
    print(message)
    # Or using f-strings (Python 3.6+):
    # message = f"Hello {name}, you are {age} years old."
    

  2. Fix the incorrect string formatting:

    item = "book"
    price = 19.99
    print("%s costs %d" % (item, price))
    

    Hint: 💡 The format specifier doesn't match the variable type. %d is for integers.

    🔍 View Issue & Fixed Solution

    Issue: Using %d for a floating-point number truncates the decimal places.

    ✅ Fixed Solution

    item = "book"
    price = 19.99
    print("%s costs %.2f" % (item, price))
    # Or using f-strings:
    # print(f"{item} costs {price:.2f}")
    

  3. Fix the incorrect string concatenation:

    first_name = "John"
    last_name = "Doe"
    full_name = first_name + last_name
    print(full_name)
    

    Hint: 💡 Missing a space between first and last name.

    🔍 View Issue & Fixed Solution

    Issue: The names are concatenated without a space, resulting in "JohnDoe".

    ✅ Fixed Solution

    first_name = "John"
    last_name = "Doe"
    full_name = first_name + " " + last_name
    print(full_name)
    

  4. Fix the code that tries to access a character in a string:

    word = "Python"
    first_char = word[0]
    last_char = word[6]
    print(first_char, last_char)
    

    Hint: 💡 Python strings are zero-indexed, and the last index is len(string) - 1.

    🔍 View Issue & Fixed Solution

    Issue: Index 6 is out of range for "Python" (valid indices are 0 to 5).

    ✅ Fixed Solution

    word = "Python"
    first_char = word[0]
    last_char = word[-1]  # or word[5]
    print(first_char, last_char)
    

  5. Fix the incorrect string slicing:

    text = "Hello, World!"
    substring = text[7:12]
    print(substring)
    

    Hint: 💡 Check the indices for "World!" in the string.

    🔍 View Issue & Fixed Solution

    Issue: The slice [7:12] gives "World" but misses the "!".

    ✅ Fixed Solution

    text = "Hello, World!"
    substring = text[7:13]  # or text[7:]
    print(substring)
    

  6. Fix the incorrect f-string usage:

    name = "Alice"
    age = 30
    print(f"Name: {name}, Age: {age}")
    

    Hint: 💡 The f-string syntax is correct, but the output format could be improved.

    🔍 View Issue & Fixed Solution

    Issue: The output is correct but lacks proper spacing/punctuation.

    ✅ Fixed Solution

    name = "Alice"
    age = 30
    print(f"Name: {name}, Age: {age}.")  # Added a period for better formatting
    

  7. Fix the incorrect .format() method usage:

    item = "coffee"
    price = 2.5
    print("The {} costs {}".format(price, item))
    

    Hint: 💡 The order of arguments in .format() matters.

    🔍 View Issue & Fixed Solution

    Issue: The placeholders and variables are in the wrong order.

    ✅ Fixed Solution

    item = "coffee"
    price = 2.5
    print("The {} costs ${}".format(item, price))
    

  8. Fix the incorrect %-formatting:

    x = 10
    y = 3
    print("Division result: %d" % (x / y))
    

    Hint: 💡 %d is for integers, but the result is a float.

    🔍 View Issue & Fixed Solution

    Issue: Using %d truncates the decimal part of the division.

    ✅ Fixed Solution

    x = 10
    y = 3
    print("Division result: %.2f" % (x / y))  # Shows 2 decimal places
    

🟡 Intermediate Fix & Find

🔴 Advanced Fix & Find

📚 Related Resources

🧠 Practice & Progress

Explore More Topics

📘 Learn Python

Tutorials, Roadmaps, Bootcamps & Visualization Projects

Python Fundamentals

Flow Control Statements


Python Functions


Fundamentals more ...




🧠 Python Advanced

Object-Oriented Programming in Python (OOP)

More...

🧠 Modules