Open In App

Python String max() Method

Last Updated : 19 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The string max() method returns the largest character in a string based on its Unicode value.

Example:

Python
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

Python
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

Python
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

Python
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.


Next Article
Article Tags :
Practice Tags :

Similar Reads