Everyone in the hall was now staring at the hat, he stared at it, too. For a few seconds, there was complete silence. Then the hat twitched. A rip near the brim opened wide like a mouth -- and the hat began to sing:

But don't judge on what you see,
I'll eat myself if you can find
A smarter hat than me.
You can keep your bowlers black,
Your top hats sleek and tall,
For I'm the Hogwarts Sorting Hat
And I can cap them all.
There's nothing hidden in your head
The Sorting Hat can't see,
So try me on and I will tell you
Where you ought to be.
You might belong in Gryffindor,
Where dwell the brave at heart,
Their daring, nerve, and chivalry Set Gryffindors apart;
You might belong in Hufflepuff,
Where they are just and loyal,
Those patient Hufflepuffis are true And unafraid of toil;
Or yet in wise old Ravenclaw,
if you've a ready mind,
Where those of wit and learning,
Will always find their kind;
Or perhaps in Slytherin
You'll make your real friends,
Those cunning folk use any means
To achieve their ends.
So put me on! Don't be afraid!
And don't get in a flap!
You're in safe hands (though I have none)
For I'm a Thinking Cap!"


The whole hall burst into applause as the hat finished its song. It bowed to each of the four tables and then became quite still again.

Harry Potter and the Philosopher's Stone - Chapter 7 : The Sorting Hat
See this quote in the Pensive -