w3resource logo


Python Exercises

Python: Python program to count the number of strings where the string length is 2 or more and the first and last character are same from a given list of strings


Write a Python program to count the number of strings where the string length is 2 or more and the first and last character are same from a given list of strings.

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

Sample Solution :

Python Code :

def match_words(words):
  ctr = 0

  for word in words:
    if len(word) > 1 and word[0] == word[-1]:
      ctr += 1
  return ctr

print(match_words(['abc', 'xyz', 'aba', '1221']))

Console :

Copy and paste the above code and press "Enter key" to execute :

Post your code through Disqus