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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:26:42+00:00 2026-05-28T05:26:42+00:00

This is what my problem is: I need to make a random string 50

  • 0

This is what my problem is: I need to make a random string 50 characters long, made up of 1s and 0s.

I know how to solve this problem, and even have a one-liner for it. I have also looked for various solutions to this problem on SO, only to get back what I already know(1, 2, etc). But what I really want is the most Pythonic way of doing this.

Currently, I’m leaning towards ''.join(( random.choice([0,1]) for i in xrange(50) ))

Is there a more pythonic way of doing this? Is there a built-in that does something like this, perhaps in itertools?

  • 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-28T05:26:43+00:00Added an answer on May 28, 2026 at 5:26 am

    For Python2.7 or better:

    In [83]: import random
    
    In [84]: '{:050b}'.format(random.randrange(1<<50))
    Out[84]: '10011110110110000011111000011100101111101001001011'
    

    (In Python2.6, use '{0:050b}' instead of '{:050b}'.)


    Explanation:

    The string.format method can convert integers into their binary string representations. The basic format code to do this is '{:b}':

    In [91]: '{:b}'.format(10)
    Out[91]: '1010'
    

    To make a string of width 50, use the format code '{:50b}':

    In [92]: '{:50b}'.format(10)
    Out[92]: '                                              1010'
    

    and to fill in the whitespace with zeros, use {:050b}:

    In [93]: '{:050b}'.format(10)
    Out[93]: '00000000000000000000000000000000000000000000001010'
    

    The syntax for str.format is a bit daunting at first.
    Here is my cheat sheet:

    http://docs.python.org/library/string.html#format-string-syntax
    replacement_field ::= "{" field_name ["!" conversion] [":" format_spec] "}"
    field_name        ::= (identifier|integer)("."attribute_name|"["element_index"]")* 
    attribute_name    ::= identifier
    element_index     ::= integer
    conversion        ::= "r" | "s"
    format_spec       ::= [[fill]align][sign][#][0][width][,][.precision][type]
    fill              ::= <a character other than '}'>
    align             ::= "<" | ">" | "=" | "^"
                          "=" forces the padding to be placed after the sign (if any)
                              but before the digits. (for numeric types)
                          "<" left justification
                          ">" right justification 
                          "^" center justification
    sign              ::= "+" | "-" | " "
                          "+" places a plus/minus sign for all numbers    
                          "-" places a sign only for negative numbers
                          " " places a leading space for positive numbers
    #                     for integers with type b,o,x, tells format to prefix
                          output with 0b, 0o, or 0x.
    0                     enables zero-padding. equivalent to 0= fill align.
    width             ::= integer
    ,                     tells format to use a comma for a thousands separator
    precision         ::= integer
    type              ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" |
                          "o" | "x" | "X" | "%"
        c convert integer to corresponding unicode character
        n uses a locale-aware separator
        % multiplies number by 100, display in 'f' format, with percent sign
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some ideas on how I can best solve this problem. I have
I have solved this problem, I just need to know what to do. I
Well i need some help here i don't know how to solve this problem.
I need some help in solving this problem. We have a large amount of
I don't like JSF but I need to solve this problem with it, I
I have this idea for a free backup application. The largest problem I need
this is the problem: inside a lib i need to print MONTHNAMES to string
This is my problem: I need to store a lot of log messages and
Need some help with this problem in implementing with XSLT, I had already implemented
ive being trying to cope with this problem but still. I need your help.

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.