Python Forum

Full Version: python gives wrong string length and wrong character
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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- 
>>> 
what encoding is this? You can encode it via encode() method
>>> len('lòng')
5
>>> len('lòng'.encode('ascii','ignore'))
4
Try this
>>> w = 'l\u00F2ng'
>>> w
'lòng'
>>> print(w)
lòng
>>> len(w)
4
Your initial string could be a mojibake.