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.
SyntaxFollowing 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.
This method returns the max alphabetical character from the string str.
ExampleFor 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: yExample
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: oExample
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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4