如何将变量的值放入字符串中(将其插入字符串中)?

发布于 2024-09-04 17:40:37 字数 1410 浏览 3 评论 0原文

我想将 int 放入 string 中。这就是我现在正在做的事情:

num = 40
plot.savefig('hanning40.pdf') #problem line

我必须为几个不同的数字运行程序,所以我想做一个循环。但是像这样插入变量是行不通的:

plot.savefig('hanning', num, '.pdf')

How do I insert a variable into a Python string?


另请参阅

如果您尝试创建文件路径,请参阅如何创建文件的完整路径文件的各个部分(例如文件夹的路径、名称和扩展名)? 了解其他技术。使用专门用于创建路径的代码通常会更好。

如果您尝试构建具有可变数据的 URL不要使用普通的字符串格式,因为它很容易出错并且比必要的更困难。可以使用专门的工具。请参阅在 Python 中向给定 URL 添加参数

如果您尝试构建 SQL查询不要使用普通字符串格式,因为它是主要的安全风险。这就是“SQL注入”的原因,“SQL注入”每年都会给真正的公司带来巨额资金年。例如,请参阅如何在 Python 中的 SQL 语句中使用变量?了解正确的技术。

如果您只想打印(输出)字符串,您可以先这样准备,或者如果您不需要该字符串做其他任何事情,使用对 print 的一次调用单独打印输出的每一部分。有关详细信息,请参阅如何在同一行上同时打印多个内容(固定文本和/或变量值)?两种方法。

请参阅如何连接 str 和 int 对象?,了解尝试使用 < 引起的错误code>+ 当其中一个不是字符串时连接“字符串”。

I would like to put an int into a string. This is what I am doing at the moment:

num = 40
plot.savefig('hanning40.pdf') #problem line

I have to run the program for several different numbers, so I'd like to do a loop. But inserting the variable like this doesn't work:

plot.savefig('hanning', num, '.pdf')

How do I insert a variable into a Python string?


See also

If you are trying to create a file path, see How can I create a full path to a file from parts (e.g. path to the folder, name and extension)? for additional techniques. It will usually be better to use code that is specific to creating paths.

If you are trying to construct an URL with variable data, do not use ordinary string formatting, because it is error-prone and more difficult than necessary. Specialized tools are available. See Add params to given URL in Python.

If you are trying to construct a SQL query, do not use ordinary string formatting, because it is a major security risk. This is the cause of "SQL injection" which costs real companies huge amounts of money every year. See for example How to use variables in SQL statement in Python? for proper techniques.

If you just want to print (output) the string, you can prepare it this way first, or if you don't need the string for anything else, print each piece of the output individually using a single call to print. See How can I print multiple things (fixed text and/or variable values) on the same line, all at once? for details on both approaches.

See How can I concatenate str and int objects? for bugs caused by trying to use + to join "strings" when one of them isn't a string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

黯淡〆 2024-09-11 17:40:38

使用 f-strings

plot.savefig(f'hanning{num}.pdf')

这是在 3.6 中添加的,是新的首选方式。


使用 str.format()

plot.savefig('hanning{0}.pdf'.format(num))

字符串连接:

plot.savefig('hanning' + str(num) + '.pdf')

转换说明符

plot.savefig('hanning%s.pdf' % num)

使用本地变量名称(巧妙的技巧):

plot.savefig('hanning%(num)s.pdf' % locals())

使用 string.Template

plot.savefig(string.Template('hanning${num}.pdf').substitute(locals()))

另请参阅:

Using f-strings:

plot.savefig(f'hanning{num}.pdf')

This was added in 3.6 and is the new preferred way.


Using str.format():

plot.savefig('hanning{0}.pdf'.format(num))

String concatenation:

plot.savefig('hanning' + str(num) + '.pdf')

Conversion Specifier:

plot.savefig('hanning%s.pdf' % num)

Using local variable names (neat trick):

plot.savefig('hanning%(num)s.pdf' % locals())

Using string.Template:

plot.savefig(string.Template('hanning${num}.pdf').substitute(locals()))

See also:

眼眸里的那抹悲凉 2024-09-11 17:40:38

随着引入格式化字符串文字(“f-strings”简而言之)在Python 3.6中,现在可以用更简洁的语法来编写:

>>> name = "Fred"
>>> f"He said his name is {name}."
'He said his name is Fred.'

根据问题中给出的示例,它看起来像这样

plot.savefig(f'hanning{num}.pdf')

With the introduction of formatted string literals ("f-strings" for short) in Python 3.6, it is now possible to write this with a briefer syntax:

>>> name = "Fred"
>>> f"He said his name is {name}."
'He said his name is Fred.'

With the example given in the question, it would look like this

plot.savefig(f'hanning{num}.pdf')
两仪 2024-09-11 17:40:38
plot.savefig('hanning(%d).pdf' % num)

% 运算符在字符串后面时,允许您通过格式代码(在本例中为 %d)将值插入到该字符串中。有关更多详细信息,请参阅 Python 文档:

printf-样式字符串格式

plot.savefig('hanning(%d).pdf' % num)

The % operator, when following a string, allows you to insert values into that string via format codes (the %d in this case). For more details, see the Python documentation:

printf-style String Formatting

不及他 2024-09-11 17:40:38

您可以使用 + 作为普通字符串连接函数以及 str()

"hello " + str(10) + " world" == "hello 10 world"

You can use + as the normal string concatenation function as well as str().

"hello " + str(10) + " world" == "hello 10 world"
挽袖吟 2024-09-11 17:40:38

一般来说,您可以使用以下方法创建字符串:

stringExample = "someString " + str(someNumber)
print(stringExample)
plot.savefig(stringExample)

In general, you can create strings using:

stringExample = "someString " + str(someNumber)
print(stringExample)
plot.savefig(stringExample)
深海里的那抹蓝 2024-09-11 17:40:38

如果您想将多个值放入字符串中,您可以使用 format

nums = [1,2,3]
plot.savefig('hanning{0}{1}{2}.pdf'.format(*nums))

将生成字符串 hanning123.pdf。这可以用任何数组来完成。

If you would want to put multiple values into the string you could make use of format

nums = [1,2,3]
plot.savefig('hanning{0}{1}{2}.pdf'.format(*nums))

Would result in the string hanning123.pdf. This can be done with any array.

三生池水覆流年 2024-09-11 17:40:38

特殊情况

根据变量数据与字符串一起使用的原因,通用方法可能不合适。

如果您需要准备 SQL 查询,

请勿使用任何组装字符串的常用技术。相反,请使用 SQL 库的功能进行参数化查询。

查询是代码,因此不应将其视为普通文本。使用该库将确保任何插入的文本都被正确转义。如果查询的任何部分可能以任何方式来自程序外部,那么恶意用户就有机会执行 SQL 注入。这被广泛认为是重要的计算机安全问题之一,每年给实体公司造成大量资金损失,并给无数客户带来问题。即使您认为您知道数据是“安全的”,使用任何其他方法也没有真正的好处。

语法取决于您正在使用的库,并且超出了本答案的范围。

如果您需要准备 URL 查询字符串,

请参阅在 Python 中向给定 URL 添加参数。不要自己做;没有任何实际的理由让你的生活变得更加困难。

写入文件

虽然可以提前准备字符串,但使用单独的 .write 调用来写入每条数据可能会更简单且内存效率更高。当然,非字符串在写入之前仍然需要转换为字符串,这可能会使代码变得复杂。这里没有一个一刀切的答案,但选择不好通常不会有太大影响。

如果您只是调用 print

内置的 print 函数接受可变数量的参数,并且可以接受任何对象并使用 str 将其字符串化>。在尝试字符串格式化之前,请考虑简单地传递多个参数是否可以达到您想要的效果。 (您还可以使用 sep 关键字参数来控制参数之间的间距。)

# display a filename, as an example
print('hanning', num, '.pdf', sep='')

当然,可能还有其他原因说明为什么程序组装字符串很有用;因此,请务必在适当的情况下这样做。

需要注意的是,print 是一种特殊情况。唯一能以这种方式工作的函数是明确编写为以这种方式工作的函数。对于普通的函数和方法,比如input,或者Matplotlib绘图的savefig方法,我们需要自己准备一个字符串。

连接

Python 支持在两个字符串之间使用 +但不支持在字符串和其他类型之间使用。要解决此问题,我们需要将其他值显式转换为字符串:'hanning' + str(num) + '.pdf'

基于模板的方法

解决问题的大多数方法都涉及使用某种“模板”字符串,其中包含显示应在何处添加信息的“占位符”,然后使用某些函数或方法来添加缺少的信息。

f-strings

如果可能的话,这是推荐的方法。它看起来像 f'hanning{num}.pdf'。要插入的变量名称直接出现在字符串中。值得注意的是,实际上并不存在“f-string”这样的东西;它不是一个单独的类型。相反,Python 会提前翻译代码:

>>> def example(num):
...     return f'hanning{num}.pdf'
... 
>>> import dis
>>> dis.dis(example)
  2           0 LOAD_CONST               1 ('hanning')
              2 LOAD_FAST                0 (num)
              4 FORMAT_VALUE             0
              6 LOAD_CONST               2 ('.pdf')
              8 BUILD_STRING             3
             10 RETURN_VALUE

因为它是一种特殊的语法,所以它可以访问其他方法中未使用的操作码。

str.format

当 f 字符串不可行时,这是推荐的方法 - 主要是因为模板字符串需要提前准备并稍后填写。它看起来像 'hanning{}.pdf'.format(num)'hanning{num}.pdf'.format(num=num)'。这里,format 是内置于字符串中的方法,它可以按位置或关键字接受参数。

特别是对于 str.format,了解内置的 localsglobalsvars 函数返回非常有用将变量名称映射到这些变量的内容的字典。因此,我们可以使用 '{ a}{b}{c}'.format(**locals())解压locals() 字典。

str.format_map

这是 .format 的罕见变体。它看起来像 'hanning{num}.pdf'.format_map({'num': num})。它不接受关键字参数,而是接受一个映射参数。

这听起来可能不太有用 - 毕竟,我们可以轻松地编写 'hanning{num}.pdf'.format_map(my_dict),而不是 'hanning{num}。 pdf'.format(**my_dict)。但是,这对于动态确定值的映射非常有用,而不是普通的 dict 。在这些情况下,使用 ** 解包可能不起作用,因为可能无法提前确定密钥集;并且尝试根据模板解压密钥是很麻烦的(想象一下:'hanning{num}.pdf'.format(num=my_mapping[num]),每个占位符都有一个单独的参数)。

string.Formatter

string 标准库模块包含一个很少使用的 Formatter 类。使用它看起来像string.Formatter().format('hanning{num}.pdf', num=num)。模板字符串再次使用相同的语法。这显然比仅仅在字符串上调用 .format 更笨拙;动机是允许用户子类化 Formatter 来为模板字符串定义不同的语法。

所有上述方法都使用通用的“格式化语言”(尽管 string.Formatter 允许更改它); {} 内还可以放入许多其他内容。解释它是如何工作的超出了这个答案的范围;请查阅文档。请记住,文字 {} 字符需要通过加倍来转义。该语法大概是受到 C# 的启发。

% 运算符

这是解决问题的传统方法,受到 C 和 C++ 的启发。长期以来一直被劝阻,但仍然受到支持。对于简单的情况,它看起来像 'hanning%s.pdf' % num。正如您所期望的,模板中的文字 '%' 符号需要加倍才能转义。

它有一些问题:

  • 看起来转换说明符(% 后面的字母)应该与正在插值的类型匹配,但实际情况并非如此。相反,该值会被转换为指定的类型,然后从那里转换为字符串。这通常是不必要的;直接转换为字符串在大多数情况下都是有效的,而首先转换为其他类型在其余大部分时间中没有帮助。因此,几乎总是使用 's'(除非您想要使用 'r' 来表示值的 repr)。尽管如此,转换说明符是语法的强制部分。

  • 元组经过特殊处理:在右侧传递元组是提供多个参数的方式。这是一个丑陋的特殊情况,但这是必要的,因为我们没有使用函数调用语法。因此,如果您确实想要将元组格式化为单个占位符,则必须将其包装在 1 元组中。

  • 其他序列类型进行特殊处理,不同的行为可能是一个陷阱。

string.Template

string 标准库模块包含一个很少使用的 Template 类。实例提供 substitutesafe_substitute 方法,其工作方式与内置 .format 类似(safe_substitute 将使占位符保持不变而不是在参数不匹配时引发异常)。这也应该被视为解决该问题的遗留方法。

它看起来像 string.Template('hanning$num.pdf').substitute(num=num),并且受到传统 Perl 语法的启发。它显然比 .format 方法更笨重,因为在方法可用之前必须使用单独的类。可以选择在变量名称周围使用大括号 ({}),以避免歧义。与其他方法类似,模板中的文字 '$' 需要加倍才能转义。

Special cases

Depending on why variable data is being used with strings, the general-purpose approaches may not be appropriate.

If you need to prepare an SQL query

Do not use any of the usual techniques for assembling a string. Instead, use your SQL library's functionality for parameterized queries.

A query is code, so it should not be thought about like normal text. Using the library will make sure that any inserted text is properly escaped. If any part of the query could possibly come from outside the program in any way, that is an opportunity for a malevolent user to perform SQL injection. This is widely considered one of the important computer security problems, costing real companies huge amounts of money every year and causing problems for countless customers. Even if you think you know the data is "safe", there is no real upside to using any other approach.

The syntax will depend on the library you are using and is outside the scope of this answer.

If you need to prepare a URL query string

See Add params to given URL in Python. Do not do it yourself; there is no practical reason to make your life harder.

Writing to a file

While it's possible to prepare a string ahead of time, it may be simpler and more memory efficient to just write each piece of data with a separate .write call. Of course, non-strings will still need to be converted to string before writing, which may complicate the code. There is not a one-size-fits-all answer here, but choosing badly will generally not matter very much.

If you are simply calling print

The built-in print function accepts a variable number of arguments, and can take in any object and stringify it using str. Before trying string formatting, consider whether simply passing multiple arguments will do what you want. (You can also use the sep keyword argument to control spacing between the arguments.)

# display a filename, as an example
print('hanning', num, '.pdf', sep='')

Of course, there may be other reasons why it is useful for the program to assemble a string; so by all means do so where appropriate.

It's important to note that print is a special case. The only functions that work this way are ones that are explicitly written to work this way. For ordinary functions and methods, like input, or the savefig method of Matplotlib plots, we need to prepare a string ourselves.

Concatenation

Python supports using + between two strings, but not between strings and other types. To work around this, we need to convert other values to string explicitly: 'hanning' + str(num) + '.pdf'.

Template-based approaches

Most ways to solve the problem involve having some kind of "template" string that includes "placeholders" that show where information should be added, and then using some function or method to add the missing information.

f-strings

This is the recommended approach when possible. It looks like f'hanning{num}.pdf'. The names of variables to insert appear directly in the string. It is important to note that there is not actually such a thing as an "f-string"; it's not a separate type. Instead, Python will translate the code ahead of time:

>>> def example(num):
...     return f'hanning{num}.pdf'
... 
>>> import dis
>>> dis.dis(example)
  2           0 LOAD_CONST               1 ('hanning')
              2 LOAD_FAST                0 (num)
              4 FORMAT_VALUE             0
              6 LOAD_CONST               2 ('.pdf')
              8 BUILD_STRING             3
             10 RETURN_VALUE

Because it's a special syntax, it can access opcodes that aren't used in other approaches.

str.format

This is the recommended approach when f-strings aren't possible - mainly, because the template string needs to be prepared ahead of time and filled in later. It looks like 'hanning{}.pdf'.format(num), or 'hanning{num}.pdf'.format(num=num)'. Here, format is a method built in to strings, which can accept arguments either by position or keyword.

Particularly for str.format, it's useful to know that the built-in locals, globals and vars functions return dictionaries that map variable names to the contents of those variables. Thus, rather than something like '{a}{b}{c}'.format(a=a, b=b, c=c), we can use something like '{a}{b}{c}'.format(**locals()), unpacking the locals() dict.

str.format_map

This is a rare variation on .format. It looks like 'hanning{num}.pdf'.format_map({'num': num}). Rather than accepting keyword arguments, it accepts a single argument which is a mapping.

That probably doesn't sound very useful - after all, rather than 'hanning{num}.pdf'.format_map(my_dict), we could just as easily write 'hanning{num}.pdf'.format(**my_dict). However, this is useful for mappings that determine values on the fly, rather than ordinary dicts. In these cases, unpacking with ** might not work, because the set of keys might not be determined ahead of time; and trying to unpack keys based on the template is unwieldy (imagine: 'hanning{num}.pdf'.format(num=my_mapping[num]), with a separate argument for each placeholder).

string.Formatter

The string standard library module contains a rarely used Formatter class. Using it looks like string.Formatter().format('hanning{num}.pdf', num=num). The template string uses the same syntax again. This is obviously clunkier than just calling .format on the string; the motivation is to allow users to subclass Formatter to define a different syntax for the template string.

All of the above approaches use a common "formatting language" (although string.Formatter allows changing it); there are many other things that can be put inside the {}. Explaining how it works is beyond the scope of this answer; please consult the documentation. Do keep in mind that literal { and } characters need to be escaped by doubling them up. The syntax is presumably inspired by C#.

The % operator

This is a legacy way to solve the problem, inspired by C and C++. It has been discouraged for a long time, but is still supported. It looks like 'hanning%s.pdf' % num, for simple cases. As you'd expect, literal '%' symbols in the template need to be doubled up to escape them.

It has some issues:

  • It seems like the conversion specifier (the letter after the %) should match the type of whatever is being interpolated, but that's not actually the case. Instead, the value is converted to the specified type, and then to string from there. This isn't normally necessary; converting directly to string works most of the time, and converting to other types first doesn't help most of the rest of the time. So 's' is almost always used (unless you want the repr of the value, using 'r'). Despite that, the conversion specifier is a mandatory part of the syntax.

  • Tuples are handled specially: passing a tuple on the right-hand side is the way to provide multiple arguments. This is an ugly special case that's necessary because we aren't using function-call syntax. As a result, if you actually want to format a tuple into a single placeholder, it must be wrapped in a 1-tuple.

  • Other sequence types are not handled specially, and the different behaviour can be a gotcha.

string.Template

The string standard library module contains a rarely used Template class. Instances provide substitute and safe_substitute methods that work similarly to the built-in .format (safe_substitute will leave placeholders intact rather than raising an exception when the arguments don't match). This should also be considered a legacy approach to the problem.

It looks like string.Template('hanning$num.pdf').substitute(num=num), and is inspired by traditional Perl syntax. It's obviously clunkier than the .format approach, since a separate class has to be used before the method is available. Braces ({}) can be used optionally around the name of the variable, to avoid ambiguity. Similarly to the other methods, literal '$' in the template needs to be doubled up for escaping.

岁月流歌 2024-09-11 17:40:38

我需要一个扩展版本:我需要生成一系列“file1.pdf”、“file2.pdf”等形式的文件名,而不是在字符串中嵌入单个数字。这就是它的方式工作:

['file' + str(i) + '.pdf' for i in range(1,4)]

I had a need for an extended version of this: instead of embedding a single number in a string, I needed to generate a series of file names of the form 'file1.pdf', 'file2.pdf' etc. This is how it worked:

['file' + str(i) + '.pdf' for i in range(1,4)]
不及他 2024-09-11 17:40:38

您可以在字符串中创建字典并替换变量。

var = {"name": "Abdul Jalil", "age": 22}
temp_string = "My name is %(name)s. I am %(age)s years old." % var

You can make dict and substitute variables in your string.

var = {"name": "Abdul Jalil", "age": 22}
temp_string = "My name is %(name)s. I am %(age)s years old." % var
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文