I'm looking for a way to identify the WiFi network to which my Raspberry Pi is connected in my program (Python) so that I can use the name of the WiFi.
iwconfig may help (man page: https://linux.die.net/man/8/iwconfig). When called with an interface name
iwconfig wlan0 it should print the current information, which also includes the ESSID -- the network name.
You may need to "sanitize" the network name when you plan to use it as a hostname: hostnames should be conforming to DNS label rules, and only consist of letters, digits, and hyphens, with some additional constraints applied (so-called LDH rule). In particular, you need to remove blanks, dots, underscores, and many ASCII characters.