Search code examples
pythonreplaceremoving-whitespace

Remove only trailing whitespace from output


I have a task that was assigned to me for homework. Basically the problem is:

Write a program that can get rid of the brand names and replace them with the generic names.

The table below shows some brand names that have generic names. The mapping has also been provided to you in your program as the BRANDS dictionary.

BRANDS = {
  'Velcro': 'hook and loop fastener',
  'Kleenex': 'tissues',
  'Hoover': 'vacuum',
  'Bandaid': 'sticking plaster',
  'Thermos': 'vacuum flask',
  'Dumpster': 'garbage bin',
  'Rollerblade': 'inline skate',
  'Asprin': 'acetylsalicylic acid'
}

This is my code:

sentence = input('Sentence: ')

sentencelist = sentence.split()
for c in sentencelist:
  if c in BRANDS:
    d = c.replace(c, BRANDS[c])
    print(d, end=' ')
  else:
    print(c, end=' ')

My output:

Sentence: I bought some Velcro shoes.
I bought some hook and loop fastener shoes.  

Expected output:

Sentence: I bought some Velcro shoes.
I bought some hook and loop fastener shoes.

It looks the same, but in my output there was an extra whitespace after 'shoes.' when there isn't supposed to be a whitespace. So how do I remove this whitespace?

I know you could do rstrip() or replace() and I tried it, but it would just jumble everything together when I just need to remove the trailing whitespace and not remove any other whitespace. If the user put the brand name in the middle of the sentence, and I used rstrip(), it would join the brand name and the rest of the sentence together.


Solution

  • The problem is that print(c, end=' ') will always print a space after c. Here is a pretty minimal change to fix that:

    sentence = input('Sentence: ')
    
    sentencelist = sentence.split()
    is_first = True
    for c in sentencelist:
        if not is_first:
            print(' ', end='')
        is_first = False
        if c in BRANDS:
            d = c.replace(c, BRANDS[c])
            print(d, end='')
        else:
            print(c, end='')
    

    As others have pointed out, this can be tidied up, e.g., d = c.replace(c, BRANDS[c]) is equivalent to d = BRANDS[c], and if you change it to c = BRANDS[c], then you could use a single print call and no else clause.

    But you also have to be careful with your approach, because it will fail for sentences like "I bought a Hoover." The sentence.split() operation will keep "Hoover." as a single item, and that will fail the c in BRANDS test due to the extra period. You could try to separate words from punctuation, but that won't be easy. Another solution would be to apply all the replacements to each element, or equivalently, to the whole sentence. That should work fine in this case since you may not have to worry about replacement words that could be embedded in longer words (e.g., accidentally replacing 'cat' embedded in 'caterpillar'). So something like this may work OK:

    new_sentence = sentence
    for brand, generic in BRANDS.items():
        new_sentence = new_sentence.replace(brand, generic)
    print(new_sentence)