Ruby 相当于 Python 的 `s= "hello, %s.%s在哪里?"%(“约翰",“玛丽")` [英] What is Ruby equivalent of Python's `s= "hello, %s. Where is %s?" % ("John","Mary")`

查看:36
本文介绍了Ruby 相当于 Python 的 `s= "hello, %s.%s在哪里?"%(“约翰",“玛丽")`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Python, this idiom for string formatting is quite common

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

Ruby 中的等价物是什么?

What is the equivalent in Ruby?

推荐答案

最简单的方法是 字符串插值.您可以将一小段 Ruby 代码直接注入到您的字符串中.

The easiest way is string interpolation. You can inject little pieces of Ruby code directly into your strings.

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

您也可以在 Ruby 中进行格式化字符串.

You can also do format strings in Ruby.

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

记得在那里使用方括号.Ruby 没有元组,只有数组,而且那些使用方括号.

Remember to use square brackets there. Ruby doesn't have tuples, just arrays, and those use square brackets.

这篇关于Ruby 相当于 Python 的 `s= "hello, %s.%s在哪里?"%(“约翰",“玛丽")`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆