This question already has answers here:
Hmm, I am pretty sure that you do not need nor really want it, but Python has provision for
computing a variable name. Simply it is a rather advanced feature and the normal way is to use mappings (
dict) or sequence (
Here, you would use:
all = 
all.append([['mortem' 'cliffi' 'gear' 'lerp' 'control']])
all.append([['video' 'player' 'stori' 'book' 'think' 'narr' 'kill']])
num = 0
BTW, this syntax is weird, because you are essentially concatenating adjacent litteral string...
But if you really, really need it you can build a interpolator of variables:
if name in locals():
elif name in globals():
raise NameError(repr(name) + ' is not defined')
You can then do:
all0 = [['mortem' 'cliffi' 'gear' 'lerp' 'control']]
all1 = [['video' 'player' 'stori' 'book' 'think' 'narr' 'kill']]
num = 0
print(getvar("all%d" % num))
and get as expected:
Python, Python | Insert a number in string encounter a problem in which we might have a numeric variable whose value keeps print ( "The string after adding number is : " + str (res)) This operator can be used to format the string to add the integer. in string · Python | Count the Number of matching characters in a pair of string Python | Insert the string at the beginning of all items in a list; numpy.insert() in Python; Python list | insert() Python | Pandas TimedeltaIndex.insert; Python MySQL - Insert into Table; Python | Pandas Index.insert() Python | Pandas dataframe.insert() Python | Insert after every Nth element in a list; MongoDB python | insert(), replace_one(), replace_many()
You can use
eval() for that.
But this is really bad style. Don't do this. You should refactor your code and use
for loops to go through your lists e.g.
all = [['mortem' 'cliffi' 'gear' 'lerp' 'control']]
all.append(['video' 'player' 'stori' 'book' 'think' 'narr' 'kill'])
for l in all:
Inserting values into strings, You can use the string method format method to create new strings with The values do not have to be strings, they can be numbers and other Python objects. is to use the new formatted string literal (f-string) syntax to insert variable values. you show where the inserted values should go using a % character followed by A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables: A variable name must start with a letter or the underscore character; A variable name cannot start with a number; A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Leaving aside how bad an idea it might be (probably
really bad), variable names are just keys in a Python dict. A one-line solution might be:
vars()[new_name] = vars().pop(old_name)
for global variables, and
vars(some_obj)[new_name] = vars(some_obj).pop(old_name)
for variables of some_obj object.
Strings and Character Data in Python – Real Python, If you were to create a string variable and initialize it to the empty string by But the ord() function will return numeric values for Unicode characters as well: >>> In this tutorial, learn how to create number variables of various types in Python. You can create integer, float and complex number variables of Python. To create these variables in Python, you have to just assign these number type values to the number variable. Learn these variable types with the examples and explanation given below.
Python Variables, You can also use the + character to add a variable to another variable: If you try to combine a string and a number, Python will give you an error: To create a string variable in Python, you have to add a sequence of character within single or double quotes. The only difference between the single quote and the double quote string is. You cannot add any single quotes character (like it’s) in the single quotes string. But in double quote string, you can add single quoted character string. String Tutorials & Notes | Python, To create a string, put the sequence of characters inside either single quotes, double quotes, or triple You can look into how variables work in Python in the Python variables tutorial. split the string "1 2 3" and return a list of the numbers. With Python, we can use the *args or **kwargs syntax to capture a variable number of arguments in our functions. Using *args, we can process an indefinite number of arguments in a function's position. With **kwargs, we can retrieve an indefinite number of arguments by their name.
Assigning Value to Python Variables, Assigning a value to a Python variable is fairly straightforward. concatenation (+) to put the strings together and an escape character A variable's value does not have to be only a character string—it also can be a number. Ways to increment a character in Python In python there is no implicit concept of data types, though explicit conversion of data types is possible, but it not easy for us to instruct operator to work in a way and understand the data type of operand and manipulate according to that.
Comments Why not have a
all with keys
1, ... Then you can get and set values from the dictionary with
num easily. (Although bear in mind that
all is a built-in function.)