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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:22:43+00:00 2026-05-26T01:22:43+00:00

For some reason I’m having a heck of a time figuring out how to

  • 0

For some reason I’m having a heck of a time figuring out how to do this in Python.
I am trying to represent a binary string in a string variable, and all I want it to have is

0010111010

However, no matter how I try to format it as a string, Python always chops off the leading zeroes, which is giving me a headache in trying to parse it out.

I’d hoped this question would have helped, but it doesn’t really…

Is there a way to force Python to stop auto-converting my string to an integer?

I have tried the following:

val = ""
if (random.random() > 0.50):
  val = val + "1"
else
  val = val + "0"

and

val = ""
if (random.random() > 0.50):
  val = val + "%d" % (1)
else:
  val = val + "%d" % (0)

I had stuck it into an array previously, but ran into issues inserting that array into another array, so I figured it would just be easier to parse it as a string.

Any thoughts on how to get my leading zeroes back? The string is supposed to be a fixed length of 10 bits if that helps.


Edit:

The code:

  def create_string(x):
    for i in xrange(10):        # 10 random populations
      for j in xrange(int(x)):  # population size
        v = ''.join(choice(('0','1')) for _ in range(10))
        arr[i][j] = v

    return arr

  a = create_string(5)
  print a

Hopefully the output I’m seeing will show you why I’m having issues:

[[  10000100 1100000001  101010110     111011   11010111]
 [1001111000 1011011100 1110110111  111011001   10101000]
 [ 110010001 1011010111 1100111000 1011100011 1000100001]
 [  10011010 1000011001 1111111010   11100110  110010101]
 [1101010000 1010110101  110011000 1100001001 1010100011]
 [  10001010 1100000001 1110010000   10110000   11011010]
 [    111011 1000111010    1100101 1101110001  110110000]
 [ 110100100 1100000000 1010101001   11010000 1000011011]
 [1110101110 1100010101 1110001110   10011111  101101100]
 [  11100010 1111001010  100011101    1101010 1110001011]]

The issue here isn’t only with printing, I also need to be able to manipulate them on a per-element basis. So if I go to play with the first element, then it returns a 1, not a 0 (on the first element).

  • 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-26T01:22:43+00:00Added an answer on May 26, 2026 at 1:22 am

    If I understood you right, you could do it this way:

    a = 0b0010111010
    '{:010b}'.format(a)
    
    #The output is: '0010111010'
    

    Python 2.7

    It uses string format method.

    This is the answer if you want to represent the binary string with leading zeros.

    If you are just trying to generate a random string with a binary you could do it this way:

    from random import choice
    ''.join(choice(('0','1')) for _ in range(10))
    

    Update

    Unswering your update.
    I made a code which has a different output if compared to yours:

    from random import choice
    from pprint import pprint
    
    arr = []
    
    def create_string(x):
        for i in xrange(10):       # 10 random populations
            arr.append([])
            for j in xrange(x):  # population size
                v = ''.join(choice(('0','1')) for _ in range(10))
                arr[-1].append(v)
        return arr
    
    a = create_string(5)
    pprint(a)
    

    The output is:

    [['1011010000', '1001000010', '0110101100', '0101110111', '1101001001'],
     ['0010000011', '1010011101', '1000110001', '0111101011', '1100001111'],
     ['0011110011', '0010101101', '0000000100', '1000010010', '1101001000'],
     ['1110101111', '1011111001', '0101100110', '0100100111', '1010010011'],
     ['0100010100', '0001110110', '1110111110', '0111110000', '0000001010'],
     ['1011001011', '0011101111', '1100110011', '1100011001', '1010100011'],
     ['0110011011', '0001001001', '1111010101', '1110010010', '0100011000'],
     ['1010011000', '0010111110', '0011101100', '1111011010', '1011101110'],
     ['1110110011', '1110111100', '0011000101', '1100000000', '0100010001'],
     ['0100001110', '1011000111', '0101110100', '0011100111', '1110110010']]
    

    Is this what you are looking for?

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
For some reason, this line of code is returning undefined for $(this).attr(href) $(a).attr(href, javascript:page('
For some reason there's a variable called d that is defined immediately after I
For some reason this isn't working, am I missing something obvious? RewriteRule ^(.*)infopopup.html$ /acatalog/infopopup.html
For some reason this doesn't work. It compiles file, but no items are added
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, I can not get this dialog to show up on the
For some reason, exceptions thrown in an __autoload function aren't being caught when trying
For some reason, I need to restart a GLSurfaceView.Renderer so I want some time
For some reason this isn't working for me. It gives me the vector iterator

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.