- Input: Prompts the user to enter a string.
- Initialization: Sets up an empty string
new_strto store the result and a variableprecharto track the last character. - Processing: Loops through each character in the input string:
- Adds the character to
new_stronly if it’s different from the last added character.
- Adds the character to
- Output: Prints the modified string with consecutive duplicates removed.
Questions
Write a python program to create a new string with no duplicate consecutive letters from a given string.
Last Updated : 26 Jan 2026
Jul 2024
Solution:
# Get input from the user
str = input("Enter any string ")
# Initialize new string and previous character
new_str = ""
prechar = None
# Loop through each character
for char in str:
# Add char to new_str if different from prechar
if char != prechar:
new_str += char
prechar = char
# Output the result
print(new_str)
OUTPUT:
Enter any string Uttam Sahu From Unnao
Utam Sahu From Unao
Code Explaination:
Meanwhile you can watch this video
Watch Video