why does islower have to be after .lower() in python

Question:

im new to coding in python

why islower() have to be after lower() i.e why is the below statement incorrect

phrase = "Hello world!"

print(phrase.islower().lower())

like am I not saying False first then change to lower????

Answers:

When you chain methods, each method is called on the result of the previous method, not the original value. So

print(phrase.islower().lower())

is roughly equivalent to

temp1 = phrase.islower()
temp2 = temp1.lower()
print(temp2)

lower() is being called on temp1, the boolean result of islower(), not the string phrase. Since booleans don’t have a lower() method, this gets an error.

Answered By: Barmar
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.