python - 什么是 Ruby 等价于 Python 的 `s= "你好,%s。 %s 在哪里 ?"% ("John","Mary")`

标签 python ruby string-formatting

在 Python 中,这种字符串格式化的习语很常见

s = "hello, %s. Where is %s?" % ("John","Mary")

Ruby 中的等价物是什么?

最佳答案

最简单的方法是string interpolation .您可以将一小段 Ruby 代码直接注入(inject)到您的字符串中。

name1 = "John"
name2 = "Mary"
"hello, #{name1}.  Where is #{name2}?"

你也可以在 Ruby 中格式化字符串。

"hello, %s.  Where is %s?" % ["John", "Mary"]

记得在此处使用方括号。 Ruby 没有元组,只有数组,并且使用方括号。

关于python - 什么是 Ruby 等价于 Python 的 `s= "你好,%s。 %s 在哪里 ?"% ("John","Mary")`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3554344/

相关文章:

python - 在 Python 中获取客户端配置文件 Azure 的访问 token

python - 在倾斜数据集的训练中使用精度和召回率

python - 密码学:将 C 函数转换为 python

ruby-on-rails - 在 Rails 中将继承转换为组合

ruby - 如何在 Ruby 的正则表达式中使用 unicode 字符?

python - Predix:没有名为 _tkinter 的模块

ruby-on-rails - Rails 完整引擎路线未加载到 Rails 4 应用程序中

python - 如何屏蔽我的 float ,最好不使用正则表达式?

python - Python中嵌套数据结构中的四舍五入小数

delphi - 当我给它一个负数时,为什么 Format with '%u' 打印出错误的值?