- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions

# Maximum length of consecutive 1’s in a binary string in Python using Map function

Sometimes when dealing with the binary representation of numbers we may be needed to find out how many continuous 1’s are present in the number. This article shows two ways how we can find out that.

## Using Split and Map

The split function in python can be used to split the given string into multiple strings. We split it by zeros and the map function is used to find the maximum length among the splits generated.

## Example

data = '11110000111110000011111010101010101011111111' def Max_len_cons_1(data): print ("Maximum Number of consecutive one's: ",max(map(len,data.split('0'))) ) Max_len_cons_1(data)

## Output

Running the above code gives us the following result −

Maximum Number of consecutive one's: 8

## Using regular expression

The re module in python can also be used to count the maximum number of consecutive 1’s. Here we find the pattern of 1+ which indicates one or more number of 1’s present. Then find the maximum length among those pattern.

## Example

data = '11110000111110010011' import re the_ones = re.findall(r"1+", data) print("The blocks of one's: ",the_ones) print("Maximum Number of consecutive one's =", len(max(the_ones, key=len)))

## Output

Running the above code gives us the following result −

The blocks of one's: ['1111', '11111', '1', '11'] Maximum Number of consecutive one's = 5

- Related Articles
- Python program to find the length of the largest consecutive 1's in Binary Representation of a given string.
- Python program using map function to find row with maximum number of 1's
- Finding maximum number of consecutive 1's in a binary array in JavaScript
- Count Binary String without Consecutive 1's
- Python program using the map function to find a row with the maximum number of 1's
- Python map function to find the row with the maximum number of 1’s
- Maximum Consecutive Increasing Path Length in Binary Tree in C++
- Maximum Consecutive Zeroes in Concatenated Binary String in C++
- Java program to find the length of the Longest Consecutive 1’s in Binary Representation of a given integer
- C# program to find the length of the Longest Consecutive 1’s in Binary Representation of a given integer
- Python program to check if there are K consecutive 1’s in a binary number?
- Maximum length of segments of 0’s and 1’s in C++
- Count number of binary strings without consecutive 1's in C
- Find maximum path length in a binary matrix in Python
- Program to find length of longest consecutive path of a binary tree in python

Advertisements