Program to replace every space in a string with hyphen | Python 3 program to replace space with - ; Get the String ; Traverse the string character by character . ; Changing the ith character to ' - ' if it 's a space. ; Print the modified string .
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " A ▁ computer ▁ science ▁ portal ▁ for ▁ geeks " NEW_LINE for i in range ( 0 , len ( str ) , 1 ) : NEW_LINE INDENT if ( str [ i ] == ' ▁ ' ) : NEW_LINE INDENT str = str . replace ( str [ i ] , ' - ' ) NEW_LINE DEDENT DEDENT print ( str ) NEW_LINE DEDENT