Python - String min() method Last Updated : 30 Dec, 2024 Comments Improve Suggest changes 7 Likes Like Report The min() function in Python is a built-in function that returns the smallest item in an iterable or the smallest of two or more arguments. When applied to strings, it returns the smallest character (based on ASCII values) from the string.Let's start with a simple example to understand how min() works with strings: Python a = "hello" smallest_char = min(a) print(smallest_char) Outpute Explanation:The string "hello" contains the characters: 'h', 'e', 'l', 'l', 'o'.'The min() function finds the smallest character based on ASCII values.The ASCII value of 'e' is smaller than that of 'h', 'l', or 'o', so 'e' is returned.Table of ContentSyntax of min() methodExamples of min() methodFrequently Asked Questions (FAQs)Syntax of min() methodmin(iterable)Parametersiterable: An iterable object (e.g., a string, list, or tuple) from which the smallest element is to be found.Return TypeReturns the smallest element of the iterable based on their ASCII or natural ordering.Raises a ValueError if the iterable is empty.Examples of min() method1. Finding the smallest character in a stringTo find the smallest character in a string is the most widely used example of min() method. Let's see how this works: Python a = "python" smallest = min(a) print(smallest) Outputh Explanation:The min() function scans through these characters and finds 'h', which has the smallest ASCII value.2. Using min() with case sensitivityThe min() function is case-sensitive and considers uppercase letters to have lower ASCII values than lowercase letters. Python a = "HelloWorld" smallest = min(a) print(smallest) OutputH Explanation:The string "HelloWorld" contains both uppercase and lowercase letters.ASCII values: 'H' has an ASCII value of 72, while lowercase letters like 'e' or 'o' have higher ASCII values.Thus, 'H' is returned.3. Applying min() to substringsThe min() function can also be applied to slices of strings. Python s = "PythonProgramming" # Analyzing "Programming" smallest = min(s[6:]) print(smallest) OutputP Explanation:The slice s[6:] extracts the substring "Programming".The smallest character based on ASCII values in "Programming" is 'P'. Create Quiz Comment S Striver Follow 7 Improve S Striver Follow 7 Improve Article Tags : Misc Python python-string Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like