python - f-strings 是 python(3.6 及更高版本)的独特功能吗?
问题描述
使用 f 字符串,您可以使用一些变量值直接在字符串中填充“占位符”。反对,让我们说使用 str.format() 方法,经常看到。
例子:
In [1]: x=3
In [2]: y=4
In [3]: print(f'The product of {x} and {y} is {x*y}.')
The product of 3 and 4 is 12.
这个 f-strings 的概念是否只在 python 中找到?或者是否有任何其他语言支持这一点,可能使用不同的名称?
解决方案
简短的回答:不 - 不是 python 独有的。在 python 中,它是由Literal String Interpolation - PEP 498在 3.6 中引入的
在 C# 中有$ - 字符串插值作为某些函数的简写,您可以将String.Format用于:
string val = "similar";
string s = $"This is {val}" // s == "This is similar"
// vs.
var s2 = string.Format ("This is {0}", val); // {0} = 0th param that follows
推荐阅读
- javascript - Vue 3 使用异步 json 设置响应式获取
- c++ - 如何使用 C++ 中的迭代器在集合内添加元素?
- javascript - 一键JS幻灯片序列
- wordpress - Weglot 与 WordPress 和 Woocommerce
- angular - 从组件内部发射到超级组件
- php - 无法定义 laravel 视图的 href 元素
- typescript - 如何从 TypeScript 中的未知类型对象中读取密钥
- java - 可选择在 Streams API 函数调用链的中间调用 .distinct()
- angular - 有没有办法完全删除 spring Security 提供的凭据页面并使用自定义身份验证提供程序进行 CSRF 实现?
- javascript - 如何使用