string length problem?

Discussion in 'Python' started by Qian Xu, Jan 7, 2009.

  1. Qian Xu

    Qian Xu Guest

    Hi All,

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

    Best regards
    Qian
    Qian Xu, Jan 7, 2009
    #1
    1. Advertising

  2. Qian Xu

    Peter Otten Guest

    Qian Xu wrote:

    > 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
    Peter Otten, Jan 7, 2009
    #2
    1. Advertising

  3. Qian Xu

    Qian Xu Guest

    Qian Xu wrote:

    > 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
    Qian Xu, Jan 7, 2009
    #3
  4. Qian Xu

    John Machin Guest

    On Jan 7, 9:40 pm, Qian Xu <-ilmenau.de> wrote:
    > 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
    John Machin, Jan 7, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mitchua
    Replies:
    5
    Views:
    2,715
    Eric J. Roode
    Jul 17, 2003
  2. =?Utf-8?B?SG96aQ==?=
    Replies:
    1
    Views:
    6,937
    Ken Cox [Microsoft MVP]
    Jun 2, 2004
  3. Sam
    Replies:
    3
    Views:
    14,081
    Karl Seguin
    Feb 17, 2005
  4. Replies:
    2
    Views:
    5,911
  5. Replies:
    5
    Views:
    655
    John W. Kennedy
    Jan 11, 2007
Loading...

Share This Page