Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从普通字符串生成f字符串_Python_Python 3.6_F String - Fatal编程技术网

Python 从普通字符串生成f字符串

Python 从普通字符串生成f字符串,python,python-3.6,f-string,Python,Python 3.6,F String,我正在尝试新的f字符串,我想知道是否有可能将普通字符串“编译”成f字符串。 因此,可以控制f字符串的计算时间,并且能够在使用f字符串之前定义f字符串 伪代码示例: a = 'normal string with some curly {inside}' inside = 'in it!' print(a.make_f_string()) >>> 'normal string with some curly in it!' 所以基本上我需要在它包含的变量之前定义f字符串。或者将

我正在尝试新的f字符串,我想知道是否有可能将普通字符串“编译”成f字符串。 因此,可以控制f字符串的计算时间,并且能够在使用f字符串之前定义f字符串

伪代码示例:

a = 'normal string with some curly {inside}'
inside = 'in it!'
print(a.make_f_string())
>>> 'normal string with some curly in it!'
所以基本上我需要在它包含的变量之前定义f字符串。或者将字符串设为f字符串

我试着和他玩,但运气不好

可能吗?到目前为止,我找到的唯一方法是使用eval(),这似乎远远不是一个好方法

eval(f"f'{a}'")
可能吗

不,除了以类似的方式进行
eval
ing或
exec
ing之外,您不能这样做


由于您试图在需要实际格式之前定义格式,因此需要的是
.format
,而不是
f
-字符串

a.format(**locals())
足够了吗?是的,所以它是a.map\u格式(locals()),我只是在到处玩,研究f字符串,看看它们能做什么,它们能用什么。。