alex1997 alex1997 - 1 month ago 5
Python Question

How to remove hyphen from compound words?

If i have a string:

txt = "Good-bye my friend"


I want it to be like this:

txt = "Good bye my friend"


What should i do?

Answer

From https://docs.python.org/2/library/string.html

string.replace(s, old, new[, maxreplace]) Return a copy of string s with all occurrences of substring old replaced by new. If the optional argument maxreplace is given, the first maxreplace occurrences are replaced.

So replace your hyphen with a space:

txt = txt.replace('-', ' ')

Comments