Python Practice › String Programs › Program 74

Longest run of zeros

EasyString Programs · 74 of 106

Write a Python program to find the longest run of consecutive 0s in a binary string.

Advertisement
Input
"10001100000111"
Output
5

Approach

  1. Split on “1” and take the longest piece.

Solution

Python
binary = "10001100000111"print(max(len(run) for run in binary.split("1")))
Output
5

Line by line

Line 2

Splitting on “1” leaves only the runs of zeros.

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Leave a Comment