- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
How to get a variable name as a string in Python?
A string is a collection of characters stored as a single value. Un like other technologies there is no need to explicitly declare strings in python (for that matter any variable), you just need to assign strings to a literal this makes Python strings easy to use.
In Python, a string is represented by the class named String. This class provides several functions and methods using which you can perform various operations on strings.
In this article, we are going to focus on how to get a variable name as a string in Python.
Variables are essentially name mappings to objects, thus this isn't something you do in Python. Modules, functions, and classes are the only Python objects with canonical names, and there is no assurance that this canonical name will have any significance in any namespace once the function or class has been declared or the module imported. These names can also be changed after the objects have been created, thus they aren't always reliable.
There are still techniques to acquire a variable's name as a string. To extract the variable name, you'll need to know the string. This is also a search for the variable name in reverse. As a result, if you have two variables with the same value, it may return either of them. The items function returns a list of all variables in the scope, together with their respective values. For python 2. x also we will use the same method items.
In the example given below, we are taking 2 variables and we are finding out the variable names using locals().items() method.
math = 75 chem = 69 variable =  variable.append([ i for i, j in locals().items() if j == math]) variable.append([ i for i, j in locals().items() if j == chem]) print("The variable names are") print(variable)
The output of the above example is,
The variable names are ['math', 'chem']
In the example given below, we are taking only a single variable and we are finding out its name.
radiation = 75.0 variable = [ i for i, j in locals().items() if j == radiation] print("The variable name is") print(variable)
The output to the above example is,
The variable name is radiation
- Related Articles
- How to get a variable name as a string in PHP?
- How to get a function name as a string in Python?
- How to check if type of a variable is string in Python?
- How to print a variable name in C?
- How to fetch an Integer variable as String in GoLang?
- How to use a variable in a string in jQuery?
- How to get the length of a string in Python?
- How to get integer values from a string in Python?
- How to get the size of a string in Python?
- How to concatenate variable in a string in jQuery?
- How to get a string from a tkinter filedialog in Python 3?