Python Forum
Strange behaviour while splitting string?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Strange behaviour while splitting string?
#1
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
Reply
#2
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ê  
Reply
#3
Thank you for your well explained examples.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  splitting file into multiple files by searching for string AlphaInc 2 816 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  logger behaviour setdetnet 1 853 Apr-15-2023, 05:20 AM
Last Post: Gribouillis
  can someone explain this __del__ behaviour? rjdegraff42 1 692 Apr-12-2023, 03:25 PM
Last Post: deanhystad
  Asyncio weird behaviour vugz 2 1,191 Apr-09-2023, 01:48 AM
Last Post: vugz
  Weird behaviour using if statement in python 3.10.8 mikepy 23 3,430 Jan-18-2023, 04:51 PM
Last Post: mikepy
  Generator behaviour bla123bla 2 1,072 Jul-26-2022, 07:30 PM
Last Post: bla123bla
  Inconsistent behaviour in output - web scraping Steve 6 2,446 Sep-20-2021, 01:54 AM
Last Post: Larz60+
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 2,651 Jan-21-2021, 02:21 PM
Last Post: InputOutput007
  Behaviour of 2D array SimonB 6 2,763 Jan-21-2021, 01:29 PM
Last Post: SimonB
  strange behaviour- plotting nathan_Blanc_Haifa 0 1,469 Dec-27-2020, 01:37 PM
Last Post: nathan_Blanc_Haifa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020