Find the maximum number in a delimited string in Python

Updated

Given K delimiter seperated String Numbers,  extracted maximum of it.

Examples :

Input : test_str = '54*345*23*45*234', delim = '*'
Output : 345
Explanation :
Maximum element between splits is 345.

Input : test_str = '547*345*23*45*234', delim = '*'
Output : 547
Explanation :
Maximum element between splits is 547.

Method #1 : Using loop + split() + int() :

In this, we split all the numbers into individuals using split(), then convert into integer using int(), maximum is found using looping and saving max at each iteration.

Implementation :

# Python3 code to demonstrate working of 
# Maximum of K separated String Numbers
# Using loop + split() + int()

# initializing string
test_str = '54:345:23:45:234'

# printing original string
print("The original string is : " + str(test_str))

# initializing K delim 
delim = ':'

res = 0

# spliting strings
for ele in test_str.split(delim):
    
    # checking for max element
    if int(ele) > res :
        res = int(ele) 

# printing result 
print("The Maximum element : " + str(res)) 
Output

The original string is : 54:345:23:45:234
The Maximum element : 345

Method #2 : Using max() + list comprehension :

This is yet another way to solve this, maximum is computed using max() and list comprehension is used to iteration, and type conversion using int().

# Python3 code to demonstrate working of 
# Maximum of K separated String Numbers
# Using max() + list comprehension

# initializing string
test_str = '54:345:23:45:234'

# printing original string
print("The original string is : " + str(test_str))

# initializing K delim 
delim = ':'

# one-liner to solve the problem
res = max([int(ele) for ele in test_str.split(delim)])

# printing result 
print("The Maximum element : " + str(res)) 
Output

The original string is : 54:345:23:45:234
The Maximum element : 345
Comment
Mithlesh Upadhyay Published 2 Dec 2026 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials