Python String max() Method



The Python string max() method compares the characters of a given string in a lexicographical manner and displays the maximum valued character among them. That means, the method internally compares the ASCII values of each character within the string and prints the highest valued character.

The max() method can be used in various ordering techniques, for example: sorting techniques. It comes in handy when a sequence of characters are to be arranged in either ascending order or descending order. It can also be used in creating tree data structures or heap data structures in Python.

Syntax

Following is the syntax for Python String max() method −

max(str)

Parameters

  • str − This is the string from which max alphabetical character needs to be returned.

Return Value

This method returns the max alphabetical character from the string str.

Example

For a simple string containing characters passed as an argument, the method returns the maximum valued character in the string.

The following example shows the usage of Python String max() method.

str = "this is really a string example";
print("Max character: " + max(str))

When we run above program, it produces following result −

Max character: y

Example

If the string argument contains alphabets and digits, the method returns the highest coded alphabet present in the string

Let us look at an example that shows the usage of the max() method on an alphanumeric string below −

str = "hello827";
print "Max character: " + max(str)

When we compile and run the above program, the output is obtained as follows −

Max character: o

Example

If the string argument contains alphabets and symbols, the method returns the highest valued alphabet present in the string.

str = "Welcome to Tutorialspoint!!!!"
print("Max character: " + max(str))

Compile and execute the program above to obtain the result as follows −

Max character: u
python_strings.htm
Advertisements