0

I made a variable.

body_weight_1 = [34, 45, 45, 65, 56]
body_weight_2 = [33, 30, 40, 50, 90]

And, I expected to print 'body'.

str(body_weight_1)[:4]

But, the result is

[34

how to get word 'body' from 'body_weight_1' in this situation?

  • Its not that easy to get the name of the variable to a string: https://stackoverflow.com/questions/18425225/getting-the-name-of-a-variable-as-a-string In your case you are casting the list into a string which looks like `'[34, 45, 45, 65, 56]'` after that. So `str(body_weight_1)[:4]` give you than the first 3 chars of the string which is `'[34'`. – micharaze Oct 04 '19 at 08:38
  • * give you than the first 4 chars of the string which is `'[34,'`. You forgot a comma in your result. – micharaze Oct 04 '19 at 09:06

1 Answers1

0

How I already commented here is that its not that easy to get the string of a virable name. Instead you can create a dictionary:

data = { 
    'body_weight_1': [34, 45, 45, 65, 56], 
    'body_weight_2': [33, 30, 40, 50, 90] 
}

for key in data :
    # prints the first 4 chars of all keys
    print(key[:4])

Output:

>> body
>> body
micharaze
  • 957
  • 8
  • 25