在字符串中嵌入表达式的Python等效项是什么? (即Ruby中的“#{expr}”))] << [

问题描述 投票:4回答:6
在Python中,我想创建一个带有嵌入式表达式的字符串块。在Ruby中,代码如下所示:

def get_val 100 end def testcode s=<<EOS This is a sample string that references a variable whose value is: #{get_val} Incrementing the value: #{get_val + 1} EOS puts s end testcode

在Python中,我想创建一个带有嵌入式表达式的字符串块。在Ruby中,代码如下所示:def get_val 100 end def testcode s = << [[
python ruby language-comparisons string-interpolation
6个回答
5
投票
Update:从Python 3.6开始,有formatted string literals (f-strings)支持文字字符串内插:f"..{get_val()+1}..."

4
投票

2
投票

2
投票

2
投票
#

1
投票
© www.soinside.com 2019 - 2024. All rights reserved.