+ 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.
4 Answers
+ 6
7 is greater than 2 ,strings are compared character by character
+ 2
Abhay I was expecting you to answer, thank you so much. I find it strange this works this way.
+ 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'
+ 1
just use int
X=['200','77']
def max_(X):
I=[int(z) for z in X]
return max(I)
print(max_(X))