infiniti ayush
infiniti ayush

Reputation: 51

How to call a string as a function in Python

I have a string A:

import keyboard

A = "keyboard.press_and_release('left windows+R')"

But, I want it to work as a function. If I call A, then it should press Windows Key and R key present on y keyboard.

Upvotes: 0

Views: 63

Answers (2)

Mureinik
Mureinik

Reputation: 311853

You can use eval:

a = "keyboard.press_and_release('left windows+R')"
eval(a)

Upvotes: 1

JDong
JDong

Reputation: 2304

Try using eval. See https://docs.python.org/3.6/library/functions.html#eval or What does Python's eval() do?

import keyboard

A = "keyboard.press_and_release('left windows+R')"
eval(A)

Upvotes: 0

Related Questions