Find out if there is a string data type in the Python list

Asked 2 years ago, Updated 2 years ago, 16 views

a=[1, 'this','a',' ']

if str in a:
    print(a)

I want to know if there's a string data type on the list Doesn't it come out if I do this?

python

2022-09-20 22:03

1 Answers

a=[1, 'this','a',' ']

# If you want to know the element that is a string,
for s in filter(lambda i: isinstance(i, str), a):
  print(s)

# If you just want to check if there is or not,
if any(isinstance(el, str) for el in a):
  print(str(a) + " has a string")


2022-09-20 22:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.