0

I am trying to automatize a process. I need to extract a string and previously assigned it a variable. For example:

H=8
Hello= "Hello"
Hi=(Hello[0])
print(H)
print(Hi)

Console prints:

8
H

and I need the console to print:

8
8
I'mahdi
  • 23,382
  • 5
  • 22
  • 30

1 Answers1

0

Use a dictionary instead.

Any method that solves this how you want it to be solved will be unsafe and bad practice.

data = {'H': 8, 'B': 3}
Hello = "Hello"
Hi = data[Hello[0]]
print(data['H'])
print(Hi)

Output:

8
8

See: Why is using 'eval' a bad practice?

BeRT2me
  • 12,699
  • 2
  • 13
  • 31