max() function for numbers in strings? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

max() function for numbers in strings?

max(['200', '77']) # Outputs: 77 Why is that? Shouldn't it be 200? or if it works like, which number comes last, it should still be 200 since it has 3 digits.

11th Nov 2020, 10:38 AM
ใ‹ใ‚“ใงใ‚“
ใ‹ใ‚“ใงใ‚“ - avatar
4 Answers
+ 6
7 is greater than 2 ,strings are compared character by character
11th Nov 2020, 10:39 AM
Abhay
Abhay - avatar
+ 2
Abhay I was expecting you to answer, thank you so much. I find it strange this works this way.
11th Nov 2020, 10:40 AM
ใ‹ใ‚“ใงใ‚“
ใ‹ใ‚“ใงใ‚“ - avatar
+ 1
You got to convert the 200 and 77 from str to int or float, otherwise it wont be seen as a number but a 'word'
11th Nov 2020, 10:41 AM
Stan Lipman
Stan Lipman - avatar
+ 1
just use int X=['200','77'] def max_(X): I=[int(z) for z in X] return max(I) print(max_(X))
11th Nov 2020, 11:45 AM
Juan Esteban
Juan Esteban - avatar