GoCoding.org

Program to extract maximum numeric value from the given string

by | Dec 26, 2020 | Python Programs

Introduction

The task is to find the maximum number from the alphanumeric string.

extract maximum numeric value

Program

import re  
    
def max_ele(ip_str):  
 
    ele = map(int,re.findall("\d+",ip_str))  
    return max(ele) 
        
ip_str = input("Enter the string: ")
print("The maximum number in the string {0} is {1}". format(ip_str, max_ele(ip_str)))

Output

extract maximum numeric value Output

Explanation

In the above python code the steps followed are:

  • We have used re.findall() method to extract the numeric value from the given string. The re.findall() method takes expression and string as the argument. We passed expression as ‘\d+’, it returns one or more digit values.
  • We have used map() function to convert the numbers to integer.
  • And returned the max element using max() function.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.