174k views
0 votes
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.

User Olejorgenb
by
8.1k points

1 Answer

5 votes

Answer:

Sure, here's a Python program that counts the number of strings where the string length is 2 or more and the first and last character are the same from a given list of strings.

```python

def count_strings(lst):

count = 0

for string in lst:

if len(string) >= 2 and string[0] == string[-1]:

count += 1

return count

# example usage

lst = ['racecar', 'hello', 'level', 'goodbye', 'noon']

count = count_strings(lst)

print(count) # output: 3

```

In this program, the `count_strings` function takes a list of strings as an argument (`lst`). It uses a for loop to iterate over each string in the list. For each string, it checks if the length of the string is 2 or greater and if the first and last character of the string are the same. If both conditions are true, it increments the count variable by 1.

Finally, the function returns the count of strings that meet both conditions.

In the example usage, we use the function to count the number of strings in the list `lst` that have a length of 2 or greater and have the same first and last character. The program outputs `3`, which is the number of strings that meet both conditions.

Step-by-step explanation:

please follow me for more if you need any help

User Eshayne
by
7.9k points