Python String max() Method
The string max() method returns the largest character in a string based on its Unicode value.
Example:
s = "hello"
res = max(s)
print(res)
Output
o
Explanation: In string "hello", the unicode values of the characters are compared and the character 'o' has the highest Unicode value.
Syntax of string max() Method
max(string)
Parameter:
- string: A string for which we want to find the largest character. It cannot be empty.
Return Type:
- Returns the largest character in the string based on Unicode. If the string is empty then it raises a ValueError.
Note: ASCII is a subset of Unicode. Unicode has many more characters like non-English letters and emojis. For ASCII characters from 0 to 127 their values are the same in Unicode.
Examples of string max() Method
Mixed Case String
s = "GeeksforGeeks"
print(max(s))
Output
s
Explanation: Here, max() function evaluates ASCII values for uppercase and lowercase letters separately. Since lowercase letters have higher ASCII values than uppercase case. So, 's' is largest character.
String with Digits and Letters
s = "abc123"
print(max(s))
Output
c
Explanation: The digits and letters are compared based on their ASCII values. Among the characters ('a', 'b', 'c', '1', '2', '3'), 'c' has the highest ASCII value.
String with Special Characters
s = "a!@#z"
print(max(s))
Output
z
Explanation: Special characters and alphabets are compared based on ASCII values. Here, 'z' has the highest ASCII value.