What's the u prefix in a Python string?

前端 未结 6 1042
南笙
南笙 2020-11-21 22:45

Like in:

u\'Hello\'

My guess is that it indicates \"Unicode\", is it correct?

If so, since when is it available?

6条回答
  •  悲哀的现实
    2020-11-21 23:19

    All strings meant for humans should use u"".

    I found that the following mindset helps a lot when dealing with Python strings: All Python manifest strings should use the u"" syntax. The "" syntax is for byte arrays, only.

    Before the bashing begins, let me explain. Most Python programs start out with using "" for strings. But then they need to support documentation off the Internet, so they start using "".decode and all of a sudden they are getting exceptions everywhere about decoding this and that - all because of the use of "" for strings. In this case, Unicode does act like a virus and will wreak havoc.

    But, if you follow my rule, you won't have this infection (because you will already be infected).

提交回复
热议问题