w3resource

Python Exercises: Replace repeated characters with single letters


Remove repeated consecutive chars.

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

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

Sample Solution-1:

Python Code:

# Define a function to remove repeated consecutive characters in a string
# and replace them with a single occurrence of the character
def test(text):
    # Initialize an empty list to store the result
    result = []
    
    # Iterate through each character in the input string
    for x in text:
        # Check if the result list is empty or the last character in the result is not equal to the current character
        if not result or result[-1] != x:
            # If true, append the current character to the result list
            result.append(x)
    
    # Join the characters in the result list to form the modified string
    return ''.join(result)

# Initialize a string with repeated consecutive characters
text = "Red Green White"
print("Original string:", text)

# Print a message indicating the removal of repeated consecutive characters
print("Remove repeated consecutive characters and replace with the single letters:")

# Call the function to remove repeated consecutive characters and print the result
print(test(text))

# Repeat the process with a different string
text = "aabbbcdeffff"
print("\nOriginal string:", text)

# Print a message indicating the removal of repeated consecutive characters
print("Remove repeated consecutive characters and replace with the single letters:")

# Call the function to remove repeated consecutive characters and print the result
print(test(text))

# Repeat the process with another different string
text = "Yellowwooddoor"
print("\nOriginal string:", text)

# Print a message indicating the removal of repeated consecutive characters
print("Remove repeated consecutive characters and replace with the single letters:")

# Call the function to remove repeated consecutive characters and print the result
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.

Sample Solution-2:

Python Code:

# Define a function to remove repeated consecutive characters in a string
# and replace them with a single occurrence of the character
def test(text):
    # Use a generator expression to select characters based on the condition
    return ''.join(text[i] for i in range(len(text)) if i == 0 or text[i - 1] != text[i])

# Initialize a string with repeated consecutive characters
text = "Red Green White"
print("Original string:", text)

# Print a message indicating the removal of repeated consecutive characters
print("Remove repeated consecutive characters and replace with the single letters:")

# Call the function to remove repeated consecutive characters and print the result
print(test(text))

# Repeat the process with a different string
text = "aabbbcdeffff"
print("\nOriginal string:", text)

# Print a message indicating the removal of repeated consecutive characters
print("Remove repeated consecutive characters and replace with the single letters:")

# Call the function to remove repeated consecutive characters and print the result
print(test(text))

# Repeat the process with another different string
text = "Yellowwooddoor"
print("\nOriginal string:", text)

# Print a message indicating the removal of repeated consecutive characters
print("Remove repeated consecutive characters and replace with the single letters:")

# Call the function to remove repeated consecutive characters and print the result
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.

For more Practice: Solve these Related Problems:

  • Write a Python program to compress a string by replacing consecutive duplicate characters with a single occurrence.
  • Write a Python program to use regular expressions to remove repeated consecutive characters from a string.
  • Write a Python program to iterate through a string and eliminate consecutive duplicates without changing the original order.
  • Write a Python program to implement a function that reduces any string with repeating characters to its condensed form.

Go to:


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

Python Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.