Python Forum
python gives wrong string length and wrong character
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python gives wrong string length and wrong character
#1
Below python gives wrong string length. The Length should be 4 and w[1] should be 'ò', but python give back letter 'o'. Please help.



>>> w ='lòng
>>> w 
'lòng'
>>> print (w)
lòng
>>> len(w)
5
>>> for ch in w:
...     print (ch + "-") 
... 
l- 
o- 
- 
n- 
g- 
>>> 
Reply
#2
what encoding is this? You can encode it via encode() method
>>> len('lòng')
5
>>> len('lòng'.encode('ascii','ignore'))
4
Recommended Tutorials:
Reply
#3
Try this
>>> w = 'l\u00F2ng'
>>> w
'lòng'
>>> print(w)
lòng
>>> len(w)
4
Your initial string could be a mojibake.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I don't know what is wrong (Python and SQL connection) shereen 2 145 4 hours ago
Last Post: menator01
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 440 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Is this a multiprocessing bug in Python - or am I doing something wrong? haimat 1 1,128 Oct-18-2023, 06:07 AM
Last Post: absalom1
  Python rule about the space character surrounding the equal sign ineuw 10 1,515 Sep-21-2023, 09:17 AM
Last Post: ineuw
  How do I handle escape character in parameter arguments in Python? JKR 6 1,039 Sep-12-2023, 03:00 AM
Last Post: Apoed2023
  Receiving this error in my "response" and causes script to return wrong status cubangt 18 1,911 Aug-13-2023, 12:16 AM
Last Post: cubangt
  Wrong type error rowan_bradley 6 1,145 Aug-07-2023, 10:44 AM
Last Post: rowan_bradley
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 961 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  np.percentile returns wrong value? AceTylercholine 2 621 Jul-13-2023, 06:59 PM
Last Post: Skaperen
  Something wrong with my code FabianPruitt 5 787 Jul-03-2023, 10:55 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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