How to get min alphabetical character from the string in Python?


A string is a collection of characters that can represent a single word or a whole sentence. Unlike Java there is no need to declare Python strings explicitly we can directly assign string value to a literal.

For manipulating and accessing strings, Python include several built in functions and methods. A string is an object of the String class, which contains multiple methods because everything in Python is an object.

In this article, we'll look at how to retrieve the minimum alphabetical character from a string in Python.

The min() function

The most common solution for this problem is to use the built in python library's min() function. While most people think of the min() method as a way to get the smallest or minimum integer or float from a list, it may also be used to get the smallest or minimum alphabetical letter from a string.

  • It accepts a string as input and returns the smallest letter of that string.

  • However, there is a drawback to this method: it compares using ASCII values, which means there may be mistakes if the text has a different casing because lower case letters have greater ASCII values.

  • To get around this, we need change the string to the same case, either upper or lower case, before finding the minimum.

Example 1

In the example below, we take a string that is entirely in upper case and apply the min operation to it to obtain the smallest alphabetical letter.

str1 = "TUTORIALSPOINT" print("The minimum alphabetical character from the string is") print(min(str1))

Output

The output of the above given program is,

The minimum alphabetical character from the string is
A

Example 2

In the example given below, we are taking a string with different cased letters, and spaces are also included. Since the min() function also uses ASCII values as a reference there will be discrepancies.

str1 = "Welcome to tutorialspoint" print("The minimum alphabetical character from the string is") print(min(str1))

Output

The output of the above given program is,

The minimum alphabetical character from the string is

Example 3

In the example given below, we are taking a string with the mixed casing so we are converting the string into the same case and then performing the min operation

str1 = "Hyderabad" str2 = str1.lower() print("The minimum alphabetical character from the string is") print(min(str2))

Output

The output of the above given program is,

The minimum alphabetical character from the string is
a

Example 4

In the example given below, we are using special characters and integers in the string and we are using the min operation on this string.

str1 = "Hyderabad@123" str2 = str1.lower() print("The minimum alphabetical character from the string is") print(min(str2))

Output

The output of the above given example is,

The minimum alphabetical character from the string is
1

Updated on: 19-Oct-2022

655 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements