3 ms·
In Python 2.7.12 >>>s = u'おはようございます' >>>print s[::-1] すまいざごうよはお The only problem with Python 2.7 strings is that the author of the article doesn't grok them
by Grue3 10y ago
In Python 2.7.12
>>>s = u'おはようございます'
>>>print s[::-1]
すまいざごうよはお
The only problem with Python 2.7 strings is that the author of the article doesn't grok them. Just use u'' instead of ''. There is also "from __future__ import unicode_literals".
- haikuginger 10y agoIn both of those cases, you're no longer using a Python 2 string, in that it's not a bytes-like object. For example, you can't write a Unicode object across a socket- it has to be converted to bytes first.
- Grue3 10y agounicode is a subtype of basestring. And it existed since the beginning of Python 2, afaik. It's extremely unfair to compare Python 3 strings to Python 2 str, when Python 3 str is just a rename of Python 2 unicode and has basically the same features. And the article doesn't even mention that Python 2 has all the same features.
- detaro 10y agoThe people complaining about "Python3 broke strings!" and who are targeted by the article likely don't use unicode objects in python2, and probably don't think they need them, if they did they probably wouldn't have such issues with the change?
- daenney 10y ago> And the article doesn't even mention that Python 2 has all the same features. Because it's not about the fact that you can achieve the same thing. What the article does is illustrate that a thing that returns 'str' when you call 'type' on it in Python 2 is not the same nor can be used in the same way as what Python 3 would call a 'str'.
- Grue3 10y agoNo, if that was the author's intention, he would've simply pointed out that "unicode" was renamed to "str", while "str" was renamed to "bytes" and is no longer considered a string. Instead, he wanted to demonstrate how Python 3 string handling is some sort of a major breakthrough when it's largely a cosmetic change.
- jjawssd 10y agoIt's a cosmetic change which happens to completely derail ill-informed Python programmers, so yeah, it's a big deal.
- daenney 10y agoThat's not a Python 2 str, that's a unicode object. Those two things are not the same. The unicode string behaviour is (indeed mostly) the same as what Python 3 calls a str. Python 2.7.13 (default, Dec 19 2016, 10:24:34) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> s = u'おはようございます' >>> type(s) <type 'unicode'> >>> s = 'おはようございます' >>> type(s) <type 'str'> Also, just doing a from __future__ import unicode_literals can cause problems in Python 2 if you're using other libraries that don't correctly handle str vs unicode or make assumptions about character encoding.