在python中将变量值插入到字符串中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3367288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
insert variable values into a string in python
提问by ricardo
As introduces a variable [i] into a string in python.
As 将变量 [i] 引入 python 中的字符串。
For example look at the following script, I just want to be able to give a name to the image, for example geo [0]. Tiff ... to geo [i]. tiff, or if you use an accountant as I can replace a portion of the value chain to generate a counter.
例如看下面的脚本,我只想给图像命名,例如 geo [0]。Tiff ... to geo [i]。tiff,或者如果您使用会计师,因为我可以替换价值链的一部分以生成计数器。
data = self.cmd("r.out.gdal in=rdata out=geo.tif")
self.dataOutTIF.setValue("geo.tif")
thanks for your answers
谢谢你的回答
采纳答案by John La Rooy
data = self.cmd("r.out.gdal in=rdata out=geo{0}.tif".format(i))
self.dataOutTIF.setValue("geo{0}.tif".format(i))
str.format(*args, **kwargs)Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces {}. Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument.
>>> "The sum of 1 + 2 is {0}".format(1+2) 'The sum of 1 + 2 is 3'See Format String Syntax for a description of the various formatting options that can be specified in format strings.
This method of string formatting is the new standard in Python 3.0, and should be preferred to the % formatting described in String Formatting Operations in new code.
New in version 2.6.
str.format(*args, **kwargs)执行字符串格式化操作。调用此方法的字符串可以包含由大括号 {} 分隔的文字文本或替换字段。每个替换字段包含位置参数的数字索引或关键字参数的名称。返回字符串的副本,其中每个替换字段都替换为相应参数的字符串值。
>>> "The sum of 1 + 2 is {0}".format(1+2) 'The sum of 1 + 2 is 3'有关可以在格式字符串中指定的各种格式选项的说明,请参阅格式字符串语法。
这种字符串格式化的方法是 Python 3.0 中的新标准,应该优先于新代码中的字符串格式化操作中描述的 % 格式化。
New in version 2.6.
回答by Donald Miner
You can use the operator %to inject strings into strings:
您可以使用运算符%将字符串注入字符串:
"first string is: %s, second one is: %s" % (str1, "geo.tif")
This will give:
这将给出:
"first string is: STR1CONTENTS, second one is geo.tif"
You could also do integers with %d:
你也可以做整数%d:
"geo%d.tif" % 3 # geo3.tif
回答by Chris Russell
You can also do this:
你也可以这样做:
name = input("what is your name?")
print("this is",+name)
回答by tburrows13
Use
用
var = input("Input the variable")
print("Your variable is " + var)
Note that varmust be a string, if it isn't, convert it to a string with var = str(var).
请注意,var必须是字符串,如果不是,则将其转换为带有var = str(var).
For example
例如
var = 5 # This is an integer, not a string
print("Var is " + str(var))
This solution is the easiest to read/understand, so better for beginners, as it is just simple string concatenation.
此解决方案最容易阅读/理解,因此更适合初学者,因为它只是简单的字符串连接。
回答by Gelineau
If you are using python 3.6+, the best solution is using f-strings:
如果您使用的是 python 3.6+,最好的解决方案是使用 f-strings:
data = self.cmd(f"r.out.gdal in=rdata out=geo{i}.tif")
self.dataOutTIF.setValue(f"geo{i}.tif")
It is the more readable and performant solution.
它是更具可读性和性能的解决方案。
回答by William Pourmajidi
If you are using python 3, then you can use F-string. Here is an example
如果您使用的是 python 3,那么您可以使用 F-string。这是一个例子
record_variable = 'records'
print(f"The element '{record_variable}' is found in the received data")
in this case, the output will be like:
在这种情况下,输出将类似于:
he element 'records' is found in the received data
在接收到的数据中找到元素“记录”

