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 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.

Python Code Editor:

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/string/python-data-type-string-exercise-106.php