Python 中的 GString

发布于 2024-07-18 03:22:58 字数 223 浏览 4 评论 0原文

Groovy 有一个 GString 的概念。 我可以编写这样的代码:

def greeting = 'Hello World'
println """This is my first program ${greeting}"""

我可以从字符串中访问变量的值。

我怎样才能在Python中做到这一点?

-- 谢谢

Groovy has a concept of GStrings. I can write code like this:

def greeting = 'Hello World'
println """This is my first program ${greeting}"""

I can access the value of a variable from within the String.

How can I do this in Python?

--
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

流星番茄 2024-07-25 03:22:58

在Python中,您必须显式传递可能变量的字典,您无法从字符串中访问任意“外部”变量。 但是,您可以使用 locals() 函数返回包含本地范围内所有变量的字典。

对于实际的替换,有很多方法可以做到(多么不Python!):

greeting = "Hello World"

# Use this in versions prior to 2.6:
print("My first programm; %(greeting)s" % locals())

# Since Python 2.6, the recommended example is:
print("My first program; {greeting}".format(**locals()))

# Works in 2.x and 3.x:
from string import Template
print(Template("My first programm; $greeting").substitute(locals()))

In Python, you have to explicitely pass a dictionary of possible variables, you cannot access arbitrary "outside" variables from within a string. But, you can use the locals() function that returns a dictionary with all variables of the local scope.

For the actual replacement, there are many ways to do it (how unpythonic!):

greeting = "Hello World"

# Use this in versions prior to 2.6:
print("My first programm; %(greeting)s" % locals())

# Since Python 2.6, the recommended example is:
print("My first program; {greeting}".format(**locals()))

# Works in 2.x and 3.x:
from string import Template
print(Template("My first programm; $greeting").substitute(locals()))
暮凉 2024-07-25 03:22:58
d = {'greeting': 'Hello World'}
print "This is my first program %(greeting)s" % d
d = {'greeting': 'Hello World'}
print "This is my first program %(greeting)s" % d
我三岁 2024-07-25 03:22:58

在 Python 2.6+ 中,您可以执行以下操作:

"My name is {0}".format('Fred')

查看 PEP 3101

In Python 2.6+ you can do:

"My name is {0}".format('Fred')

Check out PEP 3101.

慕烟庭风 2024-07-25 03:22:58

你不能完全...

我认为你真正能得到的最接近的是使用标准的基于%的替换,例如:

greeting = "Hello World"
print "This is my first program %s" % greeting

话虽如此,从Python 2.6开始有一些奇特的新类可以通过不同的方式做到这一点:查看2.6 的字符串文档,特别是从第 8.1.2 节开始,以了解更多信息。

You can't exactly...

I think the closest you can really get is using standard %-based substitution, e.g:

greeting = "Hello World"
print "This is my first program %s" % greeting

Having said that, there are some fancy new classes as of Python 2.6 which can do this in different ways: check out the string documentation for 2.6, specifically from section 8.1.2 onwards to find out more.

晨与橙与城 2024-07-25 03:22:58

如果您尝试进行模板化,您可能需要研究一下 Cheetah。 它可以让你准确地做你所说的事情,相同的语法等等。

http://www.cheetahtemplate.org/

If your trying to do templating you might want to look into Cheetah. It lets you do exactly what your talking about, same syntax and all.

http://www.cheetahtemplate.org/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文