Python: Remove all consecutive duplicates from a given string
Write a Python program to remove all characters except a specified character from a given string.
Sample Solution:
Python Code:
# Function to move spaces to front
def moveSpaces(str1):
# List comprehension to remove spaces
no_spaces = [char for char in str1 if char!=' ']
# Get number of spaces
space = len(str1) - len(no_spaces)
# Create string with required spaces
result = ' '*space
# Return moved spaces + string without spaces
return result + ''.join(no_spaces)
# Test string
s1 = "Python Exercises"
# Print original string
print("Original String:\n",s1)
# Print message
print("\nAfter moving all spaces to the front:")
# Print result
print(moveSpaces(s1))
Sample Output:
Original String: aabcdaee Removing all consecutive duplicates: abcdae
Flowchart:

Python Code Editor:
Previous: Write a Python program to move all spaces to the front of a given string in single traversal.
Next: Write a Python program to count Uppercase, Lowercase, special character and numeric values in a given string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.