How to Name Functions in Python

By Brendan Scott

Functions are extremely useful and powerful tools in your programming toolbox because they allow you to separate your program into meaningful blocks. All the built-ins in Python are functions, as is everything in the standard library.

The rules for naming a function are a lot like rules for naming a variable:

  • They must start with a letter or an underscore: _.

  • They should be lowercase.

  • They can have numbers.

  • They can be any length (within reason), but keep them short.

  • They can’t be the same as a Python keyword. They can have the same name as an existing function (including a built-in), but avoid this for now.

The function print_hello_world is very basic. It only does one thing. It can’t respond to different circumstances because no information is passing into the function when you call it.

This is fine if you need to do the same thing all the time. Functions are even more powerful, though, because you can communicate with them and they can do different things depending on the information. You could change this function and send it different messages to print.