Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6138223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:49:27+00:00 2026-05-23T17:49:27+00:00

I observed this strange behaviour of center method of str objects in python 2.7

  • 0

I observed this strange behaviour of center method of str objects in python 2.7 –

>>> print '-'.center(5,'.')        # '-' is the minus or hyphen key on the keyboard.
..-..                              # correctly centered
>>> print '─'.center(5, '.')       # '─' is NOT the minus or hyphen key on the keyboard.
.─.                                # incorrectly centered

Is this a bug in the implementation of center method or has something to do with the use of unicode characters?
How do I fix this?

[Note that this works perfectly fine in python 3.2]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:49:28+00:00Added an answer on May 23, 2026 at 5:49 pm

    In Python 2, there’s a distinction between the str and unicode types. A str is a sequence of bytes and is created with un-modified quotes. Your '─' is creating a sequence of 3 bytes which display as a single unicode character; when you ask str.center() to center those bytes in a field 5 bytes wide, it adds a single byte on either side; when printed you only get 3 glyphs.

    If you create a unicode object (with u'─' or ‘─’.decode(‘utf-8’)), you now have a sequence of 1 unicode character, which will be centered by adding 2 characters to either side.

    Python 3 doesn’t have this problem, because ordinary strings are unicode character sequences.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a common question, but the explanations found so far and observed behaviour
I just observed a strange behaviour (of course Oracle is probably supposed to behave
I observed a strange behavior == operator in java. I am trying to print
I often find this strange CDATA tag in XML files: <![CDATA[some stuff]]> I have
I've observed a strange thing about EditText widgets. If you adjust the layout_height downwards,
I have observed a strange behavior, I have a XAML Combobox (SilverLight4) declaration with
I've observed a strange behavior by parsing a string to a XElement. First, here
I've no experience in making something move in my app. And I observed this
I'm having a strange problem. I have a method that sends off notifications by
I've observed this in Firefox-3.5.7/Firebug-1.5.3 and Firefox-3.6.16/Firebug-1.6.2 When I fire up Firebug: var x

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.