Dremendo Tag Line

Input a sentence and print those words which begins and ends with same alphabet in Python

String Methods - Question 6

In this question, we will see how to input a sentence and print those words which begins and ends with same alphabet in Python programming. To know more about string methods click on the string methods lesson.

Q6) Write a program in Python to input a sentence and print those words which begins and ends with same alphabet.

Program

w=""
str=input("Enter a sentence\n")
str=str+" "

for i in range(0,len(str)):
    if str[i]!=' ':
        w=w+str[i]
    else:
        if w[0]==w[len(w)-1]:
            print(w)
        w=""

Output

Enter a sentence
My mom and dad love me so much
mom
dad
video-poster