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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:01:37+00:00 2026-06-14T07:01:37+00:00

I have a variable conts which contains the contents of a binary file (read

  • 0

I have a variable conts which contains the contents of a binary file (read it with rb as mode). Now when I try to get pieces of the string via slicing I would expect to see the proper length for the variables (and obviously the contents, too), but I don’t.

Here is code with which I can reproduce this and I am out of ideas what is going on here:

hdr1, hdr2 = conts[0:6], conts[10:7]
print "----------------"
print len(conts)
print len(hdr1)
print len(hdr2)
print len(conts)
print "----------------"
print type(hdr1)
print type(hdr2)
print type(conts)

The output I get is however:

----------------
32174321
6
0
32174321
----------------
<type 'str'>
<type 'str'>
<type 'str'>

What’s going on here? I thought slicing would create a new string for each slice?

Python version: 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]

Note: the four-byte gap between the slices is intentional. The problem is rather that the second slice returns a zero-length string, even though there would be enough data. I found no documentation that pieces (when slicing) need to be adjacent.

Edit: after realizing my error: I intended to get from a string longer than 17 bytes the bytes 0 to 6 and the bytes 10 to 17.

  • 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-14T07:01:38+00:00Added an answer on June 14, 2026 at 7:01 am

    The reason this is an empty string is because you didn’t mention that you wanted a negative step for the slicing, assuming you did mean to slice backwards.

    hdr1, hdr2 = conts[0:6], conts[10:7:-1]
    

    Will do what you want, notice the -1 that lets python know that you want to step backwards when slicing instead of forwards.

    As an aside, this also leads to an easy way to reverse strings:

    reverse = string[::-1]
    

    The format of slicing is:

    [start_index:end_index:step]


    From your edit I can see that this answer may be redundant for your purpose, but it is good information for you to know so I will leave it up.
    Here is how you would do what you said in your edit:

    hdr1, hdr2 = conts[0:6], conts[10:17]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read file from user using FileReference. I have a variable which I want
I have variable WCHAR sDisplayName[1024]; How can I check if sDisplayName contains the string
i have variable $lang which convert string to the selected language but i am
I have a text file stored as a string variable. The text file is
i have a pointer or variable which stores the address like 0xb72b218 now i
I have an imput .txt file, which contains a date. The date is in
I have variable which I populate from JSON formated data. Something like: var time=my_data[data.results[i].id].time;
I am allocating myMDD in main which contains an NSMutableArray instance variable (alloc/init-ed in
I have a global variable: const std::string whiteSpaceBeforeLeadingCmntOption = WhiteSpaceBeforeLeadingComment; When I remove the
I have a table which contains count for each variables inside each group. Age

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.