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
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
  Ask again if wrong guess with While. EduPy 4 108 Yesterday, 07:46 PM
Last Post: menator01
  Regex: a string does not starts and ends with the same character Melcu54 5 534 Jul-04-2021, 07:51 PM
Last Post: Melcu54
  [solved] unexpected character after line continuation character paul18fr 4 601 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  VS Code debugger using wrong Python environment topfox 0 386 Jun-09-2021, 10:01 AM
Last Post: topfox
  python error: bad character range \|-t at position 12 faustineaiden 0 492 May-28-2021, 09:38 AM
Last Post: faustineaiden
  No module named 'pysolar' - even tough pysolar is installed - What am I doing wrong? Jghurt 5 975 May-08-2021, 07:03 PM
Last Post: Jghurt
  For loops returning wrong output Nighthound21 1 613 May-03-2021, 03:43 AM
Last Post: deanhystad
  Reducing JSON character count in Python for a Twitter Bot johnmitchell85 2 703 Apr-28-2021, 06:08 PM
Last Post: johnmitchell85
  It says there is no keras in jupyter notebook. it is wrong there is a keras install Led_Zeppelin 0 528 Apr-27-2021, 08:29 PM
Last Post: Led_Zeppelin
  Message is going at wrong time quest 0 517 Apr-07-2021, 01:35 PM
Last Post: quest

Forum Jump:

User Panel Messages

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