4
4
-----
5
5
6
6
The :class: `str ` object in Python 3 is quite similar but not identical to the
7
- Python 2 :class: `unicode ` object. The major differences are:
7
+ Python 2 :class: `unicode ` object.
8
8
9
- - repr of unicode strings in Py2 is "u'...'" versus "'...'"
10
- - stricter type-checking in Py3 to enforce the distinction between unicode
11
- strings and byte-strings, such as when comparing, concatenating, joining, or
12
- replacing parts of strings.
9
+ The major difference is the stricter type-checking with Py3 ``str `` to enforce
10
+ the distinction between unicode strings and byte-strings, such as when
11
+ comparing, concatenating, joining, or replacing parts of strings.
12
+
13
+ There are also minor differences, such as the ``repr `` of unicode strings in
14
+ Py2 having a ``"u'...'" `` prefix, versus simply ``"'...'" ``, and the removal of
15
+ the :func: `str.decode ` method in Py3.
13
16
14
17
``future `` contains a backport of the :mod: `str ` object from Python 3 which
15
18
inherits from the Python 2 :class: `unicode ` class but has customizations to
@@ -46,8 +49,9 @@ behaviours to Python 3's :class:`str`::
46
49
>>> assert list(s) == ['A', 'B', 'C', 'D']
47
50
>>> assert s.split('B') == ['A', 'CD']
48
51
49
- Currently the easiest way to ensure identical use of strings in a Py3/2
50
- codebase is to wrap string literals in a :func: `~str ` call, as follows::
52
+ Currently the easiest way to ensure identical use of (unicode) strings across
53
+ Py3 and Py2 in a single-source codebase is to wrap string literals in a
54
+ :func: `~str ` call, as follows::
51
55
52
56
from __future__ import unicode_literals
53
57
from future.builtins import *
@@ -59,7 +63,7 @@ codebase is to wrap string literals in a :func:`~str` call, as follows::
59
63
# ...
60
64
61
65
Most of the time this is unnecessary, but the stricter type-checking of the
62
- ``future.builtins.str `` object, in particular, may be useful while ensuring
63
- that Py2's unicode strings are not mixed up with implicit coercions from
64
- byte-strings .
66
+ ``future.builtins.str `` object, in particular, may be useful for ensuring
67
+ the same consistent separation between unicode and byte strings on Py2 as on
68
+ Py3 .
65
69
0 commit comments