# About the str class

**URL:** <https://discuss.python.org/t/about-the-str-class/19766>\
**Category:** Python Help\
**Tags:** help\
**Created:** [October 7, 2022, 11:01am UTC](https://discuss.python.org/t/about-the-str-class/19766 "2022-10-07T11:01:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![roberthuang](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/roberthuang/32/9253_2.png) [@roberthuang](https://discuss.python.org/u/roberthuang)\
**Post date:** [October 7, 2022, 11:01am UTC](https://discuss.python.org/t/about-the-str-class/19766/1 "2022-10-07T11:01:05Z")

</div>

Can I use isinstance(something, str) to decide whether “something” is a string. I have just read in one book that said this is wrong, but I do not know why. Any reviews will be highly appreciated.

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [October 7, 2022, 11:23am UTC](https://discuss.python.org/t/about-the-str-class/19766/2 "2022-10-07T11:23:37Z")

</div>

Yes, you can use `isinstance(something, str) to decide whether `something` is a string.

I don’t know why the book says it is wrong. Do they give a reason?

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [October 7, 2022, 2:02pm UTC](https://discuss.python.org/t/about-the-str-class/19766/3 "2022-10-07T14:02:24Z")

</div>

Perhaps it’s because it could be a subclass of `str`?

---

<div class="post-metadata">

**Author:** ![komoto48g](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/komoto48g/32/6978_2.png) [@komoto48g](https://discuss.python.org/u/komoto48g)\
**Post date:** [October 7, 2022, 4:00pm UTC](https://discuss.python.org/t/about-the-str-class/19766/4 "2022-10-07T16:00:25Z")

</div>

Hi Robert,

I guess your book was written in PY2’s time?  
You can use `isinstance(something, str)` in PY3 but `isinstance(something, (str, unicode))` in PY2, or `isinstance(something, six.string_types)` if you want to keep compatibility with PY2x3.

| TYPE | PY2 | PY3 |
| --- | --- | --- |
| Unicode string | unicode | str |
| Bytes string | str(=bytes) | bytes |

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [October 7, 2022, 11:25pm UTC](https://discuss.python.org/t/about-the-str-class/19766/5 "2022-10-07T23:25:31Z")

</div>

Which would normally be just fine, and is a primary reason why  
`isinstance` is _strongly_ recommended over, say `type(thing) is str`.  
They mean different things, and you should write what you mean. But  
_usually_ in Python we want to know “can I use it like a `str`?”, and  
`isinstance` is the better choice.

Cheers,  
Cameron Simpson [cs@cskk.id.au](mailto:cs@cskk.id.au)

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [October 8, 2022, 12:07pm UTC](https://discuss.python.org/t/about-the-str-class/19766/6 "2022-10-08T12:07:06Z")

</div>

> [@](#):
>
> Perhaps it’s because it could be a subclass of `str`?

`isinstance` works correctly when it comes to subclasses.

If B is a subclass of A, then instances of B are also considered instances of A. That’s part of the definition of subclassing 🙂

To be concrete:

```python
class MyString(str):
    pass

s = MyString()
assert isinstance(s, str)

```

The alternatives I can think of are:

- `type(s) is str` which can be used to test that s is a _direct_ instance of str, not a subclass;
- duck-typing and [EAFP](https://realpython.com/python-lbyl-vs-eafp/);
- perhaps the book is thinking about wanting to handle bytes and strings together?
- or is thinking about [UserString](https://docs.python.org/3/library/collections.html#collections.UserString).

Without knowing what the book says, we’re just guessing.

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [October 8, 2022, 5:10pm UTC](https://discuss.python.org/t/about-the-str-class/19766/7 "2022-10-08T17:10:13Z")

</div>

That was my point, whether it’s a string of class `str` exactly and not of a subclass.

---

<div class="post-metadata">

**Author:** ![jakubhajek91](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jakubhajek91/32/8647_2.png) [@jakubhajek91](https://discuss.python.org/u/jakubhajek91)\
**Post date:** [October 8, 2022, 8:45pm UTC](https://discuss.python.org/t/about-the-str-class/19766/8 "2022-10-08T20:45:23Z")

</div>

[About the str class - #4 by komoto48g](https://discuss.python.org/t/about-the-str-class/19766/4) MyString(str):  
pass

s = MyString()  
assert isinstance(s, str)
