![]() |
VOOZH | about |
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:
e
Explanation:
"hello" contains the characters: 'h', 'e', 'l', 'l', 'o'.'min() function finds the smallest character based on ASCII values.'e' is smaller than that of 'h', 'l', or 'o', so 'e' is returned.min(iterable)ValueError if the iterable is empty.To find the smallest character in a string is the most widely used example of min() method. Let's see how this works:
h
Explanation:
min() function scans through these characters and finds 'h', which has the smallest ASCII value.min() with case sensitivityThe min() function is case-sensitive and considers uppercase letters to have lower ASCII values than lowercase letters.
H
"HelloWorld" contains both uppercase and lowercase letters.'H' has an ASCII value of 72, while lowercase letters like 'e' or 'o' have higher ASCII values.'H' is returned.min() to substringsThe min() function can also be applied to slices of strings.
P
[6:] extracts the substring "Programming"."Programming" is 'P'.