Python Forum
python gives wrong string length and wrong character - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: python gives wrong string length and wrong character (/thread-21810.html)



python gives wrong string length and wrong character - thienson30 - Oct-15-2019

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- 
>>> 



RE: python gives wrong string length and wrong character - metulburr - Oct-15-2019

what encoding is this? You can encode it via encode() method
>>> len('lòng')
5
>>> len('lòng'.encode('ascii','ignore'))
4



RE: python gives wrong string length and wrong character - Gribouillis - Oct-15-2019

Try this
>>> w = 'l\u00F2ng'
>>> w
'lòng'
>>> print(w)
lòng
>>> len(w)
4
Your initial string could be a mojibake.