Public toilets are not common and not always too pleasant. The easiest option is to wander into a bar or cafe and use its facilities. The polite thing to do is to make a small purchase, but you’re unlikely to raise too many eyebrows if you don’t. That said, some curmudgeonly places in popular tourist areas post notices saying that their toilets are for customers only.
The cautious carry some toilet paper with them when out and about as many toilets don't have it. If there’s a bin beside the toilet, put paper and so on in it (there may be a sign in Spanish, English and German telling telling you to do so) – probably because the local sewage system has trouble coping.