Python Practice › String Programs › Program 58
Random binary string
Write a Python program to generate a random binary string of a given length.
Input
8
Output
00110011
Approach
- Pick “0” or “1” for each position.
Solution
import random random.seed(3)n = 8print("".join(random.choice("01") for _ in range(n)))Output
00110011
Line by line
Line 3
The seed makes the output repeatable; remove it for a new string each run.
Learn the concept
New to this topic? Read the lesson, then come back.