string length problem?

Q

Qian Xu

Hi All,

why the code
print len(u"»test«")
returns 8 instead of 6?

Best regards
Qian
 
P

Peter Otten

Qian said:
why the code
print len(u"»test«")
returns 8 instead of 6?

You may have declared an encoding that differs from the one your editor
actually uses, e. g.

# -*- coding: iso-8859-1 -*-
print len(u"»test«")

whereas your editor writes UTF-8.

Peter
 
Q

Qian Xu

Qian said:
Hi All,

why the code
print len(u"»test«")
returns 8 instead of 6?

Best regards
Qian

I have solved the problem myself.

# -*- coding: utf-8 -*-
print len(u"»test«")

--- or ---
s = "»test«"
print len(s.decode("utf-8"))

--Qian
 
J

John Machin

Hi All,

why the code
  print len(u"»test«")
returns 8 instead of 6?

I suggest that you do
print repr(u"»test«")
and inspect the result.
HTH,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top