Search code examples
pythonregexpython-re

Replace more than two dots as unicode ellipsis


How do I replace two or more periods in a string with a unicode ellipsis?

e.g.

Hey. -> Hey. (does not change)
Hey.. -> Hey…  (all of  these change)
Hey... -> Hey…
Hey.... -> Hey…
Hey..... -> Hey…

Solution

  • This will do it. It uses a regular expression to match two or more dots.

    import re
    s='Hey.....'
    new_string = re.sub(r'[.][.][.]*', '…', s)
    print(new_string)