Printing family emoji, with U+200D zero-width joiner, directly, vs via list

风流意气都作罢 提交于 2019-12-04 09:53:31

The difference, as noted in the comments, is that print(family) calls the str.__str__ method, while print([family]) calls str.__repr__, which escapes non-printable unicode characters.

  1. The print function converts its (non-keyword) arguments using str.

  2. Calling str on containers (generally) calls repr on their items. Mostly this is because strings inside a container would too easily disturb the presentation of the container itself (e.g. with newlines). A PEP to change this was raised around the release of Python 3 but quickly rejected.

  3. Calling repr on strings escapes any non-printable characters (but, as of Python 3, preserves other non-ASCII Unicode characters): see PEP-3138 and the description of str.isprintable

Return true if all characters in the string are printable or the string is empty, false otherwise. Nonprintable characters are those characters defined in the Unicode character database as “Other” or “Separator”, excepting the ASCII space (0x20) which is considered printable. (Note that printable characters in this context are those which should not be escaped when repr() is invoked on a string. It has no bearing on the handling of strings written to sys.stdout or sys.stderr.)

The CPython implementation can be found here (search for the unicode_repr function).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!