livepython.net

208. Python Program to Remove Repeated Characters from a String

MediumPhase 2 · Practice

This one takes a few steps. It uses a for loop over a string, lower() and not in, and you know all three.

Overview

Removing repeated characters is a common interview question about strings. The hard part is to keep the characters in the order they first appear.

Problem Statement

Diego spells messages with letter stickers. One sticker works for both a capital and a small letter. Diego wants to know which stickers a message needs.

  • Keep the first copy of each character. Leave out every later copy.
  • Capital and small letters count as the same character: A and a are one character. Keep the first copy just as it was typed.
  • Keep the characters in the order they first appear.
  • The space is a character too: only the first space stays.
  • Print Result: <text>.

Examples

Example 1. Input programming gives:

Result: progamin

Why: the second r, the second g and the second m are left out. The other letters keep their order.

Example 2. Input Banana Bread gives:

Result: Ban red

Why: the later a and n letters in Banana are left out. In Bread, the B matches the first B, and the a was already seen, so both are left out too.

Input

One line of text (1 character or more). Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Output

One line: Result:, a space, and the text without repeats. The tests try 8 different inputs, also text with several spaces and with capital letters.

Try it first. The solution opens when you solve this question, or after 3 tries with Submit.

Finish this chapter firstChapter 12 · Working With Strings
solution.py
# write your code here

EXPECTED OUTPUT

1Result: progamin

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.