How to find out if a Python object is a string?

前端 未结 14 1964
天涯浪人
天涯浪人 2020-11-30 17:47

How can I check if a Python object is a string (either regular or Unicode)?

14条回答
  •  伪装坚强ぢ
    2020-11-30 18:16

    In order to check if your variable is something you could go like:

    s='Hello World'
    if isinstance(s,str):
    #do something here,
    

    The output of isistance will give you a boolean True or False value so you can adjust accordingly. You can check the expected acronym of your value by initially using: type(s) This will return you type 'str' so you can use it in the isistance function.

提交回复
热议问题