Write a Python program to check the first and last character are same from a given list of strings


Sample List: ['abc', 'xyz', 'aba', '1221']
Expected Result: 2 :

The program is checking the list of words called "word" and it is counting the number of words that have more than 1 character and the first character is equal to the last character. The variable "ch" is used to keep track of the number of words that meet this criteria. The program goes through each word in the list using a for loop, and checks the length of the word using the len() function. If the length of the word is greater than 1 and the first character is equal to the last character, then the variable "ch" is incremented by 1. Finally, the program prints the value of "ch" which represents the number of words in the list that meet the criteria.

Source Code

"""
Sample List: ['abc', 'xyz', 'aba', '1221']
Expected Result: 2
"""
word=["madem","3643","apple","3756"]
ch = 0
for w in word:
	if len(w) > 1 and w[0] == w[-1]:
	  ch += 1
print(ch)

Output

2

Example Programs