Python - String min() method Last Updated : 30 Dec, 2024 Comments Improve Suggest changes Like Article 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'. Comment More infoAdvertise with us Next Article Python - String min() method S Striver Follow Improve Article Tags : Misc Python python-string Practice Tags : Miscpython Similar Reads Python String Methods Python string methods is a collection of in-built Python functions that operates on strings.Note: Every string method in Python does not change the original string instead returns a new string with the changed attributes. Python string is a sequence of Unicode characters that is enclosed in quotatio 5 min read Python String find() Method find() method in Python returns the index of the first occurrence of a substring within a given string. If the substring is not found, it returns -1. This method is case-sensitive, which means "abc" is treated differently from "ABC". Example:Pythons = "Welcome to GeekforGeeks!" index = s.find("Geekf 2 min read Python String index() Method The index() method in Python is used to find the position of a specified substring within a given string. It is similar to the find() method but raises a ValueError if the substring is not found, while find() returns -1. This can be helpful when we want to ensure that the substring exists in the str 2 min read Python String rfind() Method Python String rfind() method returns the rightmost index of the substring if found in the given string. If not found then it returns -1.ExamplePythons = "GeeksForGeeks" print(s.rfind("Geeks"))Output8 Explanationstring "GeeksForGeeks" contains the substring "Geeks" twice.rfind() method starts the sea 4 min read Python String rindex() Method Python String rindex() method returns the highest index of the substring inside the string if the substring is found. Otherwise, it raises ValueError.Let's understand with the help of an example:Pythons = 'geeks for geeks' res= s.rindex('geeks') print(res)Output10 Note: If start and end indexes are 2 min read Like