w3resource

Python Exercises: Replace repeated characters with single letters

Python String: Exercise-106 with Solution

Write a Python program to remove repeated consecutive characters and replace with the single letters and print new updated string.

Sample Data:
("Red Green White") -> "Red Gren White"
("aabbbcdeffff") -> "abcdef"
("Yellowwooddoor") -> "Yelowodor"

Sample Solution-1:

Python Code:

def test(text):
  result = []
  for x in text:
    if not result or result[-1] != x:
      result.append(x)
  return ''.join(result)

text ="Red Green White"
print("Original string:", text)
print("Remove repeated consecutive characters and replace with the single letters:")
print(test(text))
text = "aabbbcdeffff"
print("\nOriginal string:", text)
print("Remove repeated consecutive characters and replace with the single letters:")
print(test(text))
text = "Yellowwooddoor"
print("\nOriginal string:", text)
print("Remove repeated consecutive characters and replace with the single letters:")
print(test(text))

Sample Output:

Original string: Red Green White
Remove repeated consecutive characters and replace with the single letters:
Red Gren White

Original string: aabbbcdeffff
Remove repeated consecutive characters and replace with the single letters:
abcdef

Original string: Yellowwooddoor
Remove repeated consecutive characters and replace with the single letters:
Yelowodor

Flowchart:

Flowchart: Replace repeated characters with single letters.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Sample Solution-2:

Python Code:

def test(text):
    return ''.join(text[i] for i in range(len(text)) if i==0 or text[i-1]!=text[i])
text ="Red Green White"
print("Original string:", text)
print("Remove repeated consecutive characters and replace with the single letters:")
print(test(text))
text = "aabbbcdeffff"
print("\nOriginal string:", text)
print("Remove repeated consecutive characters and replace with the single letters:")
print(test(text))
text = "Yellowwooddoor"
print("\nOriginal string:", text)
print("Remove repeated consecutive characters and replace with the single letters:")
print(test(text))

Sample Output:

Original string: Red Green White
Remove repeated consecutive characters and replace with the single letters:
Red Gren White

Original string: aabbbcdeffff
Remove repeated consecutive characters and replace with the single letters:
abcdef

Original string: Yellowwooddoor
Remove repeated consecutive characters and replace with the single letters:
Yelowodor

Flowchart:

Flowchart: Replace repeated characters with single letters.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous Python Exercise: Extract the name from an Email address.
Next Python Exercise: Two strings contain three letters at the same index.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Python: Tips of the Day

Getting sorted iterable (can sort by "compare" function):

>>> a = [1, 2, -3]
>>> sorted(a)
[-3, 1, 2]

>>> sorted(a,key=abs)
[1, 2, -3]