w3resource

Python: Find maximum length of consecutive 0’s in a given binary string

Python String: Exercise-64 with Solution

Write a Python program to find the maximum length of consecutive 0's in a given binary string.

Visual Presentation:

Python String: Find maximum length of consecutive 0’s in a given binary string.

Sample Solution:

Python Code:

# Define max_consecutive_0 function 
def max_consecutive_0(input_str):

# Split string on '1' and map length of each substring  
# Returns max length of 0's
   return max(map(len,input_str.split('1'))) 

# Test string  
str1 = '111000010000110'

# Print original string 
print("Original string:" + str1)  

# Print max consecutive 0's
print("Maximum length of consecutive 0’s:")
print(max_consecutive_0(str1))

# Another test string
str1 = '111000111' 

# Print original string
print("Original string:" + str1)

# Print max consecutive 0's  
print("Maximum length of consecutive 0’s:")
print(max_consecutive_0(str1)) 

Sample Output:

Original string:111000010000110
Maximum length of consecutive 0’s:
4
Original string:111000111
Maximum length of consecutive 0’s:
3

Flowchart:

Flowchart: Find maximum length of consecutive 0’s in a given binary string

Python Code Editor:

Previous: Write a Python program to remove leading zeros from an IP address.
Next: Write a Python program to find all the common characters in lexicographical order from two given lower case strings. If there are no common letters print “No common characters”.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/string/python-data-type-string-exercise-64.php