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

  • SEARCH
  • Home
  • 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 4327818
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:35:30+00:00 2026-05-21T09:35:30+00:00

I am handling a situation where I need to make a string fit in

  • 0

I am handling a situation where I need to make a string fit in the allocated gap in the screen, as I’m using unicode len() and slices[] work apparently on bytes and I end up cutting unicode strings too short, because € only occupies one space in the screen but 2 for len() or slices[].

I have the encoding headers properly setup, and I’m willing to use other things than slices or len() to deal with this, but I really need to know how many spaces will the string take and how to cut it to the available.

$cat test.py
# -*- coding: utf-8 -*-
a = "2 €uros"
b = "2 Euros"
print len(b)
print len(a)
print a[3:]
print b[3:]

$python test.py
7
9
��uros
uros
  • 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-21T09:35:30+00:00Added an answer on May 21, 2026 at 9:35 am

    You’re not creating Unicode strings there; you’re creating byte strings with UTF-8 encoding (which is variable-length, as you’re seeing). You need to use constants of the form u"..." (or u'...'). If you do that, you get the expected result:

    % cat test.py
    # -*- coding: utf-8 -*-
    a = u"2 €uros"
    b = u"2 Euros"
    print len(b)
    print len(a)
    print a[3:]
    print b[3:]
    % python test.py 
    7
    7
    uros
    uros
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running into this situation where I need to parse a String into an
I have situation when I need to communicate with Java process somehow. Java handling
What's the best method for handling a situation where you have an ASP.Net Dropdownlist
How would you reccommend handling RSS Feeds in ASP.NET MVC? Using a third party
I'm contemplating two different class designs for handling a situation where some repositories are
I'm using Spring Security which works great to make sure that a user has
This is a question related to how people are handling a situation in the
I want to ask about what your approach looks like handling a situation like
I've gotten myself into a situation in PHP where I need to access a
I'm in a rather peculiar situation right now. To make a long story short,

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.