Replace all occurrences of pi with 3.14 in a given string | Function to replace all occurrences of pi in a given with 3.14 ; Iterate through second last element of the string ; If current and current + 1 alphabets form the word ' pi ' append 3.14 to output ; Append the current letter ; Return the output string ; Driver Code
def replacePi ( input ) : NEW_LINE INDENT output = " " ; NEW_LINE size = len ( input ) ; NEW_LINE for i in range ( size ) : NEW_LINE INDENT if ( i + 1 < size and input [ i ] == ' p ' and input [ i + 1 ] == ' i ' ) : NEW_LINE INDENT output += "3.14" ; NEW_LINE i += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT output += input [ i ] ; NEW_LINE DEDENT DEDENT return output ; NEW_LINE DEDENT input = "2 ▁ * ▁ pi ▁ + ▁ 3 ▁ * ▁ pi ▁ = ▁ 5 ▁ * ▁ pi " ; NEW_LINE print ( replacePi ( input ) ) ; NEW_LINE