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 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.
Issue: Trying to concatenate strings with integers using the + operator causes a TypeError.
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."
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.
Issue: Using %d for a floating-point number truncates the decimal places.
item = "book"
price = 19.99
print("%s costs %.2f" % (item, price))
# Or using f-strings:
# print(f"{item} costs {price:.2f}")
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.
Issue: The names are concatenated without a space, resulting in "JohnDoe".
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name)
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.
Issue: Index 6 is out of range for "Python" (valid indices are 0 to 5).
word = "Python"
first_char = word[0]
last_char = word[-1] # or word[5]
print(first_char, last_char)
Fix the incorrect string slicing:
text = "Hello, World!"
substring = text[7:12]
print(substring)
Hint: 💡 Check the indices for "World!" in the string.
Issue: The slice [7:12] gives "World" but misses the "!".
text = "Hello, World!"
substring = text[7:13] # or text[7:]
print(substring)
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.
Issue: The output is correct but lacks proper spacing/punctuation.
name = "Alice"
age = 30
print(f"Name: {name}, Age: {age}.") # Added a period for better formatting
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.
Issue: The placeholders and variables are in the wrong order.
item = "coffee"
price = 2.5
print("The {} costs ${}".format(item, price))
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.
Issue: Using %d truncates the decimal part of the division.
x = 10
y = 3
print("Division result: %.2f" % (x / y)) # Shows 2 decimal places
Tutorials, Roadmaps, Bootcamps & Visualization Projects