🚀 Hurry! Offer Ends In
00 Days
00 Hours
00 Mins
00 Secs
Enroll Now
X
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:

  1. Input: Prompts the user to enter a string.
  2. Initialization: Sets up an empty string new_str to store the result and a variable prechar to track the last character.
  3. Processing: Loops through each character in the input string:
    • Adds the character to new_str only if it’s different from the last added character.
  4. Output: Prints the modified string with consecutive duplicates removed.
Report an error

Meanwhile you can watch this video

Watch Video
Latest Updates