当我遇到f字符串时,我正在阅读有关python 3.6的改进。
shepherd = "Martha"
age = 34
stuff_in_string = f"Shepherd {shepherd} is {age} years old."
print(stuff_in_string)
此代码输出带有Shepherd Martha is 34 years old.
和sheperd
变量值的字符串age
。
我想知道Python是否具有软件包或简单的pythonic方式来进行其他操作。也就是说,给定一个特定的字符串,我如何检测它内部是否包含变量,并且在这种情况下,要求用户即时输入值?
运行类似的内容
stuff_in_string = "Shepherd {shepherd} is {age} years old."
result = check_variables(stuff_in_string)
if result is not None:
answers = {}
for variable in result:
answers = get_variable_input(variable)
print(answers)
结果类似:
{'shepherd': 'Louise', 'age': '31'}