简体   繁体   中英

How to check if some datatype is string or list in python?

I'm trying to add a check here that if the receiver is just a string. then convert that into list else just pass.

  if type(receiver) == str:
        receiver=[receiver]

error: TypeError: 'str' object is not callable

You can check the type of an instance with the following.

if isinstance(receiver, str):
    # do something if it is a string

If you just need to check rather it is not a string:

if not isinstance(receiver, str):
    # do something if it is not a string

Look at this tutorial to learn more about the function.

a = '123'
print(str(a))
print(type(a))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM