Writing a function that will replace every duplicate character of the first letter of a string with * except for the first letter itself - is there a more pythonic/elegant way to do it than this (lists, etc?)?
def specialreplace(s):
firstchar = s[0]
modifiedstr = s[1:].replace(firstchar, "*")
print firstchar + modifiedstr
specialreplace('oompaloompa') ---> o*mpal**mpa
It's a simple problem, I'm not sure why you're trying to complicate it. Your solution looks good, except for the fact that you should use .join() instead of '+' to join strings together.
"".join((s[0], s[1:].replace(s[0], "*"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With