Free Python course. Sign Up for tracking progress →

Python: Syntactic Sugar

Constructions like index = index + 1 are often found in Python, so the creators of the language added an abbreviated version: index += 1.

The only difference is how they're written. The interpreter will turn an abbreviated construction into its expanded form.

These abbreviations are called syntactic sugar because they make the process of writing code a little easier and more pleasant.

There are abbreviated forms for all arithmetic operations and for string concatenation:

  • a = a + 1a += 1
  • a = a - 1a -= 1
  • a = a * 2a *= 2
  • a = a / 1a /= 1

Instructions

Implement a function called filter_string() that takes a string and a character as input, and returns a new string with the character removed from every point in the string. Try not to use the built-in methods for working with the string in your solution.

text = 'If I look back I am lost'
filter_string(text, 'I')  # 'f  look back  am lost'
filter_string(text, 'o')  # 'If I lk back I am lst'