15,994,549 members
See more:
I want to know how many strings are there in the given list

`list = [56, Tree, 776, 89, Five, One, 43, Apple]`

By looking at the list we can say there are 4 strings in the list

What I have tried:

`print(list.count(type(str)))`

But no success
Posted
Updated 27-Sep-22 4:56am
v2
Afzaal Ahmad Zeeshan 8-Oct-21 19:26pm
None of them are strings; they are variables? If not, then they are invalid characters in Python.

## Solution 1

This isn't perfect at all, but ...
Python
```a = [56, "Tree", 776, 89, "Five", "One", 43, "Apple"]
print([x for x in a if not str(x).isdigit()])```

## Solution 3

Elaborating (plagiarizing) Griff's answer...
Python
```a = [56, "Tree", 776, 89, "Five", "One", True, (True, "Goo"),  "Apple"]
b = [x for x in a if isinstance(x, str)]
print(len(b))```

v2

## Solution 2

c=0
for i in list:
if type(i)==str:
c=c+1
print("The Numbers of strings is",c)

## Solution 4

list1 = [56, 'Tree', 776, 89, 'Five', 'One', 43, 'Apple']
str_num = sum(type(x) == str for x in list1) #str_num is the number of strings in list1
print(str_num)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 60 merano99 30 RickZeeland 10 Dave Kreskowiak 10 rac8006 10
 OriginalGriff 431 Pete O'Hanlon 215 CPallini 110 Dave Kreskowiak 80 CHill60 42

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900