Use a dictionary; variables = {"q": 0, "a": 0} def stats(choice): variables[choice] += 1 stats("q") Answer from abessman on discuss.python.org
Discussions

how do i change variable name?

Programmatically, yes, but it is not recommended.

You've described what you want to do but not why. If you care to share more, we could probably advise better ways of achieving your objective.

In the absence of other information, the obvious alternative approach would be to use a dictionary.

More on reddit.com
🌐 r/learnpython
5
0
September 15, 2022
Python script to rename variables in a Python code
I can use the string replacement feature of my text editor to replace x with y, but if I only want to rename instances of the variable x to y, that will make more changes than I want. Does anyone know of a Python script to rename variables? More on discuss.python.org
🌐 discuss.python.org
5
0
March 30, 2023
Change a variable name in all places in Python - Stack Overflow
I have written a code in Python3 using the IDE Wing, but I want to change a variable name, e.g., var_1, to var_2 in all places in my file. In MATLAB, when you change a variable name in one place, by More on stackoverflow.com
🌐 stackoverflow.com
How to change the variable name all at once?
I know that ctrl + F will find the texts that I want, and I can change variable names accordingly. However, is there any function or buttons that supports changing all the variable name all at once, as we do in other non-browser based IDEs? More on discourse.jupyter.org
🌐 discourse.jupyter.org
5
0
October 28, 2021
🌐
Sololearn
sololearn.com › en › Discuss › 1604705 › solved-how-do-i-rename-a-variable-in-python
[SOLVED] how do i rename a variable in python? | Sololearn: Learn to code for FREE!
why you want to rename a variable? you can just change it backspace and then type whatever you want ... you can rename a variable like a kid shown below... x=2 y=x print(y) same output when your print x or y ... Kaveis let me try to give you ...
🌐
Reddit
reddit.com › r/learnpython › how do i change variable name?
how do i change variable name? : r/learnpython
September 15, 2022 - ... Sorry, this post was deleted by the person who originally posted it. Share ... In the editor just rename it. Otherwise you can simply assign or copy the variable: test = 0 test1 = test # Assigns both variable names to the same Object test1 = test.copy() # Creates a new object for test1
🌐
Python Forum
python-forum.io › thread-23500.html
Changing a variable's name on each iteration of a loop
Is it possible in python to change the name of a variable on each iteration of a loop? For example: for i in range(10): variableNameToChange+i='iterationNumber=='+str(i)I know this won't work, and you can't assign to an operator, but how would y...
Find elsewhere
🌐
Quora
quora.com › How-can-I-change-the-name-of-a-variable-in-a-loop-in-Python
How to change the name of a variable in a loop in Python - Quora
Answer (1 of 13): Hello! I have a good and bad news for you. I start with the bad one. Producing a new variable name at each iteration of a for loop isn’t a good idea. So I will give you a way to use Python `list` instead, to achieve the same thing. But now the good one: what you wanted to do a...
🌐
Quora
quora.com › Is-there-a-way-to-automatically-change-all-variable-and-function-names-in-Python
Is there a way to automatically change all variable and function names in Python? - Quora
Answer (1 of 4): Since most programs in nearly ANY language begin as a text file generally written with a text editor with features slanted to that language - you can probably do a simple ‘search and replace’ operation (unlike repeal-and-replace). Just backup the source code as this can ...
🌐
JetBrains
jetbrains.com › help › pycharm › rename-refactorings.html
Rename refactorings | PyCharm Documentation
Use the Rename refactoring in PyCharm to easily change the names of various symbols and update all references to them in your code.
🌐
GeeksforGeeks
geeksforgeeks.org › python › changing-variable-names-with-python-for-loops
Changing Variable Names With Python For Loops - GeeksforGeeks
July 23, 2025 - In this example, below Python code below adds the prefix "var_" to each variable name in the list original_names. It uses a for loop to iterate through the original names, creates new names with the prefix, and appends them to the list prefixed_names.
🌐
JetBrains
jetbrains.com › pycharm › guide › tips › rename-symbol
Rename Symbol - JetBrains Guide
February 17, 2023 - Simply put your cursor on a symbol and use Refactor | Rename ⌃T (macOS) / Shift+Ctrl+Alt+T (Windows/Linux) and then '1' Provide the new name, preview the places it found, and select Do Refactor. Change your mind?
🌐
OARC Stats
stats.oarc.ucla.edu › spss › faq › how-can-i-write-a-python-program-to-rename-variables-in-spss
How can I write a Python program to rename variables in SPSS? | SPSS FAQ
In this example, we will write a Python function called renamefun to rename the variables. This function will have four arguments. The location and name of the dataset will be given in datapath; the first variable to be renamed will be given in mystart; the last variable to be renamed will be given in myend; the suffix to be used to rename the variables will be given in suffix.
🌐
Reddit
reddit.com › r/learnpython › how to fix bad variable names on large codebase?
r/learnpython on Reddit: How to fix bad variable names on large codebase?
January 8, 2024 -

Hello everyone,

So, as a software developer at my first ever job i was required to make some windows and linux native softwares to collect data from machine sensors(like temprature , load,machine status etc) which are all made in python using PyQt5 framework also dashboards frontend to visualize collected data in javascript HTML and CSS , in the past 5 months i have been working on these softwares alone and there is no one at my senior level who knows coding and no teammates and there are no code reviews no documentations they just required me to implement all the features that they required, so after writing almost 7000-8000 lines of code learning by myself whereever i got stuck i realised the variable names in my code is really really bad , and whenever i look at my own code it looks like some 5 year old kid wrote those variable names and it really depresses me , i never followed any one naming convention in my code , so i would choose any one of these naming method and not following a single one , for example if i want to write a variable for reading delay i would write

readingDelay or reading_delay or ReadingDelay or readingdelay

and in my whole project its repeated everywhere , also my code works great in production and the upper management is happy with it as they just dont know any programming stuff and only look at the end result

i feel really bad about this how can i fix it? i have been coding for only last 6 months and this is my 1st project which i am getting paid so i want to give my best at it

sorry for my bad english

🌐
Sololearn
sololearn.com › en › Discuss › 3029838 › change-variable-name-at-every-iteration
Change variable name at every iteration | Sololearn: Learn to code for FREE!
May 9, 2022 - I don't think there is any way to create variables dynamically in python but you can use dictionaries to achieve same sort of behaviour. col_dict = {} for i in range(4): var = "col" + str(i+1) col_dict[var] = [] Here, i have create an empty dict and inside, there are key and values with keys being column names (created with the logic you needed) and values are just empty lists.
🌐
Real Python
realpython.com › python-variables
Variables in Python: Usage and Best Practices – Real Python
January 12, 2025 - Variables in Python are symbolic names pointing to objects or values in memory. You define variables by assigning them a value using the assignment operator. Python variables are dynamically typed, allowing type changes through reassignment.