How to name a variable using another variable? [duplicate]

I want to name a variable in python using the name of an existing variable. For example:

s = "Hello World"
words = s.split()
numofwords = len(words)
word(numofwords) = words[numofwords-1]

I'm not sure what syntax I would use to define the variable in line 4. What my code would hopefully do is print the last word in the string, stored as the variable wordx, x being the number of the last word. (In this case it would be 2.)I'm new to Python so there are probably some mistakes in the code.



Read more here: https://stackoverflow.com/questions/64401543/how-to-name-a-variable-using-another-variable

Content Attribution

This content was originally published by Theo at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: