Recipe 1.3. Testing Whether an Object Is String-like(Python Cookbook)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 >>> def isAString(anobj):
2 return isinstance(anobj, basestring)
3
4 >>> a = "hello"
5 >>> isAString(a)
6 True
7 >>> b = 't'
8 >>> isAString(b)
9 True
10 >>> c = []
11 >>> isAString(c)
12 False
13 >>> class MyString(str):
14 pass
15
16 >>> d = MyString()
17 >>> isAString(d)
18 True
2 return isinstance(anobj, basestring)
3
4 >>> a = "hello"
5 >>> isAString(a)
6 True
7 >>> b = 't'
8 >>> isAString(b)
9 True
10 >>> c = []
11 >>> isAString(c)
12 False
13 >>> class MyString(str):
14 pass
15
16 >>> d = MyString()
17 >>> isAString(d)
18 True