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 7062357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:35:03+00:00 2026-05-28T04:35:03+00:00

In [1]: str=’美’ In [2]: str.encode(‘utf-8′) Out[2]: b’\xe7\xbe\x8e’ In [3]: str.encode(‘utf-16′) Out[3]: b’\xff\xfe\x8e\x7f’ In

  • 0
In [1]: str='美'

In [2]: str.encode('utf-8')
Out[2]: b'\xe7\xbe\x8e'

In [3]: str.encode('utf-16')
Out[3]: b'\xff\xfe\x8e\x7f'

In [4]: str.encode('ascii')
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
/Users/XXXuserXXXTemp/<ipython-input-4-c7b96e3e54a7> in <module>()
----> 1 str.encode('ascii')

UnicodeEncodeError: 'ascii' codec can't encode character '\u7f8e' in position 0: ordinal not in range(128)

The str is a Chinese/Japanese character.

  • why ascii does not work?

  • how to understand Out[2] and Out[3], i.e. what they really are?

  • 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-28T04:35:04+00:00Added an answer on May 28, 2026 at 4:35 am

    Why ascii does not work?

    str='美' is not an ASCII character, it’s outside the ASCII range and therefore can’t be represented as an ASCII character.

    From the Unicode tutorial for python:

    Encodings don’t have to handle every possible Unicode character, and most encodings don’t. For example, Python’s default encoding is the ‘ascii’ encoding. The rules for converting a Unicode string into the ASCII encoding are simple; for each code point:

    1. If the code point is < 128, each byte is the same as the value of the code point.

    2. If the code point is 128 or greater, the Unicode string can’t be represented in this encoding. (Python raises a UnicodeEncodeError exception in this case.)


    how to understand Out[2] and Out[3], i.e. what they really are?

    They are byte strings (not character strings). Out[2] is the sequence of bytes which represents the 美 codepoint in UTF-8 code units. The notation \xe7 means a byte with the hexadecimal value e7. Out[3] is the sequence of bytes which represents the 美 codepoint in UTF-16 code units.

    To understand the distinction between characters, bytes, and code units, read the Unicode tutorial for python carefully and completely. For another, fairly good, treatment of the same material, read Joel Spolsky’s The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!). You should know this much, no excuses!

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

Sidebar

Related Questions

if(str.contains(final)) { id = Integer.parseInt(str.substring(18, str.length() - 6)); } System.out.println(id); This line of code
Have got an NSString *str = @12345.6789 and want to find out if there
Given unsigned char *str , a UTF-8 encoded string, is it legal to write
str = "&&"; label1.Text = str; but it only shows "&". How do I
String str = abcde123_92qwq_1a_02x_1e; I want to replace the first string part between the
string str = one three; string::iterator it; string add = two ; Lets say
function escCtrlChars(str) { return str.replace(/[\0\t\n\v\f\r\xa0'!-]/g, function(c) { return '!' + c.charCodeAt(0) + '!'; });
static void Main() { string str; str = Console.ReadLine(); while (str != null)//HERE! {
str = '' for i in self.obj: str += '[' + self.obj[i] + ']';
str is of the following pattern: 1 abc (1 <something>) For example: 1 abc

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.