How to check if a variable exists in Python

Опубликовано: 27 Февраль 2026
на канале: Code with Renan
1,065
10

You can easily check if a variable exists in Python in either local or global scope.

To check if a variable exists in the global scope, verify the name of the variable as a string between quotes and use the in operator using the globals() function.

To check if a variable exists in the local scope, you also verify the name of the variable as a string and check it against the locals() function.

You can also read about it if you prefer on:
https://renanmf.com/check-variable-ex...


📚 Buy my book Python Fundamentals
https://renanmf.com/products/


📸Instagram:   / renanmouraf  
🌎 Website: https://renanmf.com/
🐦 Twitter:   / renanmouraf  
📝 LinkedIn:   / renanmouraf  

👍 Please leave a LIKE and SUBSCRIBE for more content! 👍



⭐️ Tags ⭐️
Python

#️⃣ Hashtags #️⃣
#Python