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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:03:49+00:00 2026-06-13T23:03:49+00:00

I am playing around with the bytearray type in python 2.7.3 and noticed this

  • 0

I am playing around with the bytearray type in python 2.7.3 and noticed this weird behaviour:

Passing an integer argument to its constructor creates a bytearray of size equal to the integer filled with zero bytes:

>>> s = bytearray(15)
>>> s
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')

However, if I attempt to create a bytearray with bytes set manually by me, the constructor seems to ignore/skip some of the bytes I create:

>>> s = bytearray(b'\x34\x78\x98\xFF\xFF')
>>> s
bytearray(b'4x\x98\xff\xff')

Why is this happening? Is this a bug? Or am I missing something? The python documentation is not very enlightening.

  • 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-06-13T23:03:50+00:00Added an answer on June 13, 2026 at 11:03 pm

    Nothing strange is happening, all the bytes are still there. Relax! 🙂

    The confusion stems from the fact that the representation is slightly optimized: bytes whose values represent printable characters are printed as those characters. This makes them take just a single character in the output rather than four.

    It’s doing just what it says:

    • \x34 is the character 4, which is why the buffer starts with a ‘4‘
    • \x78 is the character x, which is why there’s an ‘x‘ after the ‘4‘
    • \x98\xff\xff are all kept as-is.

    Note that len(s) prints 5.

    Epic graphical representation:

        +---+---+------+------+------+
    s = | 4 | x | \x98 | \xff | \xff |
        +---+---+------+------+------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Playing around with Python - tkInter - Entry widget - when I use validatecommand
After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>
Been playing around with this for a couple of hours and can't seem to
While playing around with the emulator, I noticed that when trying to view a
playing around with a new samsung tablet i noticed they have mini apps on
Playing around with jQuery a bit (sorry, complete noob) I was wondering why this
When playing around with multiprocessing I noticed that in the following script, __del__ is
While playing around with playframework yabe-siena-gae, I noticed a datastore file created for the
Im playing around with the soundcloud api , in its instructions it says to
I'm playing around with writing a web app. In this case, I'm using scotty

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.