- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Python has great libraries to manipulate data. We may come across a need to find the maximum product of two consecutive numbers which are part of the big string. In this article we will see the ways to achieve that.

We convert the string into a list. Then create pairs from the consecutive elements with help of slicing. Applying * we multiply the pair and then take the max value from the result of the multiplication from each of the pairs.

Astring = '5238521' # Given string print("Given String : ",Astring) # Convert to list Astring = list(Astring) print("String converted to list:\n",Astring) # Using max() res = max(int(a) * int(b) for a, b in zip(Astring, Astring[1:])) # Result print("The maximum consecutive product is : " ,res)

Running the above code gives us the following result −

Given String : 5238521 String converted to list: ['5', '2', '3', '8', '5', '2', '1'] The maximum consecutive product is : 40

We take a similar approach as above. But we use map function to keep generating the pair of consecutive integers. Then use the mul function from operator module to multiply the numbers in this pair. Finally apply the max function to get the max value of the result.

from operator import mul Astring = '5238521' # Given string print("Given String : ",Astring) # Convert to list Astring = list(Astring) print("String converted to list:\n",Astring) # Using max() res = max(map(mul, map(int, Astring), map(int, Astring[1:]))) # Result print("The maximum consecutive product is : " ,res)

Running the above code gives us the following result −

Given String : 5238521 String converted to list: ['5', '2', '3', '8', '5', '2', '1'] The maximum consecutive product is : 40

- Related Questions & Answers
- Maximum Product Subarray in Python
- Maximum sum and product of the M consecutive digits in a number in C++
- Program to find maximum subarray min-product in Python
- Maximum Product Subarray | Added negative product case in C++
- Maximum element in tuple list in Python
- Program to find maximum product of contiguous subarray in Python
- Maximum Product of Word Lengths in C++
- Maximum Product of Three Numbers in C++
- Maximum and Minimum Product Subsets in C++
- Maximum Product Cutting | DP-36 in C++
- Maximum consecutive repeating character in string in C++
- Python – Sort by Maximum digit in Element
- Python - Sort Matrix by Maximum Row element
- Product of all pairwise consecutive elements in an Arrays in C++
- Maximum consecutive 1s after n swaps in JavaScript

Advertisements