How to get max alphabetical character from the string in Python?\n


A string is a group of characters that may be used to represent a single word or an entire phrase. Strings are simple to use in Python since they do not require explicit declaration and may be defined with or without a specifier.

Python provides a variety of built in functions and methods for manipulating and accessing strings in the class named string.

In this article, we are going to find out how to get the max alphabetical character from the given string in python.

The max() function

The most used approach for this problem is the max() function from the inbuilt python library. Everyone would assume that this function is used only for finding out the maximum number or float from a given list but, it can also be used to find out the largest or max alphabetical character from a given string.

  • It takes a string as input and returns the largest letter from it.

  • But there is one problem with this approach, this compares by using ASCII values, so there may be some errors when the string is of different casing because the lower case letters have higher ASCII values.

  • To overcome this, we should convert the string into the same case either Upper case or lower case before computing max.

Example 1

In the below given example, we are taking a string that is entirely in the upper case and we are performing the max operation on that string and we are getting the maximum alphabetical character.

str1 = "WELCOME TO TUTORIALSPOINT" print("The maximum alphabetical character from the string is") print(max(str1))

Output

The output of the above given example is,

The maximum alphabetical character from the string is
W

Example 2

In the example given below, we are taking the string input with only one capital letter and the rest of them are lower case and performing the max operation. There would be a discrepancy in the final answer because of the ASCII values.

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

Output

The output of the above program is,

The maximum alphabetical character from the string is
u

Example 3

In the example given below, we are taking the same input as before but we are rectifying our mistake by changing the case of the string before using the max operation.

str1 = "Welcome to tutorialspoint" str2 = str1.upper() print("The maximum alphabetical character from the string is") print(max(str2))

Output

The output of the above program is,

The maximum alphabetical character from the string is
W

Example 4

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

str1 = "Welcome to tutorialspoint@123" str2 = str1.upper() print("The maximum alphabetical character from the string is") print(max(str2))

Output

The output of the above string is,

The maximum alphabetical character from the string is
W

Updated on: 19-Oct-2022

958 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements