Answer to Question #307111 in Python for ratul

Question #307111

Given a string, create a new string with all the consecutive duplicates removed.

Hint: You may make a new string to store the result. You can check whether the current character and the next character are the same, then add that character to the new string.

Note: Only consecutive letters are removed not all duplicate occurences of a letter. You may try doing this alternative i.e. removing all duplicate letters from a given string, for your own practice.


1
Expert's answer
2022-03-09T10:24:48-0500
s = input()
res = s[0]

for ch in s[1:]:
    if ch != res[-1]:
        res += ch

print(res)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS