Python Forum

Full Version: Strange behaviour while splitting string?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Can somebody explain why this is happening? I'm only getting this while executing this in python2.7.
See code below.

data = "ëeêfdsf"
print data[:3]


Expected output: ëeê
Current output: ëe
Put u in front u"ëeêfdsf".
Stop using Python 2,it's dead in the end of this year.

Unicode was one biggest changes moving to Python 3.
In Python 3 is all text Unicode bye default.
Python 3.7:
>>> data = "ëeêfdsf"
>>> data
'ëeêfdsf'
>>> data[:3]
'ëeê'
Python 2.7:
>>> data = "ëeêfdsf"
>>> data
'\xc3\xabe\xc3\xaafdsf'
>>> data[:3]
'\xc3\xabe'
>>> print(data[:3])
ëe
>>> print(data[:3].decode('utf-8'))
ëe
>>> data = u"ëeêfdsf"
>>> print(data[:3])
ëeê  
Thank you for your well explained examples.