Print the first and last character of each word in a String | Python3 program to print the first and last character of each word in a String ; Function to print the first and last character of each word . ; If it is the first word of the string then print it . ; If it is the last word of the string then also print it . ; If there is a space print the successor and predecessor to space . ; Driver code
' NEW_LINE def FirstAndLast ( string ) : NEW_LINE INDENT for i in range ( len ( string ) ) : NEW_LINE INDENT if i == 0 : NEW_LINE INDENT print ( string [ i ] , end = " " ) NEW_LINE DEDENT if i == len ( string ) - 1 : NEW_LINE INDENT print ( string [ i ] , end = " " ) NEW_LINE DEDENT if string [ i ] == " ▁ " : NEW_LINE INDENT print ( string [ i - 1 ] , string [ i + 1 ] , end = " " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " Geeks ▁ for ▁ Geeks " NEW_LINE FirstAndLast ( string ) NEW_LINE DEDENT