Python 简明教程

Python - String Formatting

String formatting 在 Python 中是通过在已存在的字符串中插入数字表达式的值动态地创建字符串表现形式的过程。Python 的字符串连接操作符不接受非字符串操作数。因此,Python 提供了以下字符串格式化技术 -

String formatting in Python is the process of building a string representation dynamically by inserting the value of numeric expressions in an already existing string. Python’s string concatenation operator doesn’t accept a non-string operand. Hence, Python offers following string formatting techniques −

Using % operator

“%”(modulo)运算符通常被称作字符串格式化运算符。它将格式化字符串与一组变量结合起来,并将它们组合成一个包含按指定方式格式化的变量值的字符串。

The "%" (modulo) operator often referred to as the string formatting operator. It takes a format string along with a set of variables and combine them to create a string that contain values of the variables formatted in the specified way.

Example

要使用 “%” 运算符将一个字符串插入到格式化字符串中,我们使用 "%s",如下面的示例所示 -

To insert a string into a format string using the "%" operator, we use "%s" as shown in the below example −

name = "Tutorialspoint"
print("Welcome to %s!" % name)

它将生成以下 output

It will produce the following output

Welcome to Tutorialspoint!

Using format() method

它是 str 类的内建方法。format() 方法通过使用大括号“{}”在字符串中定义占位符来工作。然后,这些占位符会被方法参数中指定的值替换。

It is a built-in method of str class. The format() method works by defining placeholders within a string using curly braces "{}". These placeholders are then replaced by the values specified in the method’s arguments.

Example

在下面的示例中,我们使用 format() 方法动态地将值插入到字符串中。

In the below example, we are using format() method to insert values into a string dynamically.

str = "Welcome to {}"
print(str.format("Tutorialspoint"))

在运行以上代码时,它将产生以下 output -

On running the above code, it will produce the following output

Welcome to Tutorialspoint

Using f-string

f-strings(也称为格式化字符串文本)用于在字符串文本内嵌入表达式。f-strings 中的“f”表示格式化(formatted),用它作前缀将创建一个 f-string。字符串中的大括号“{}”将充当由变量、表达式或函数调用填充的占位符。

The f-strings, also known as formatted string literals, is used to embed expressions inside string literals. The "f" in f-strings stands for formatted and prefixing it with strings creates an f-string. The curly braces "{}" within the string will then act as placeholders that is filled with variables, expressions, or function calls.

Example

以下示例说明了 f-strings 与表达式一起使用的方式。

The following example illustrates the working of f-strings with expressions.

item1_price = 2500
item2_price = 300
total = f'Total: {item1_price + item2_price}'
print(total)

以上代码的输出如下所示 -

The output of the above code is as follows −

Total: 2800

Using String Template class

String Template 类属于 string 模块,它提供了一种通过使用占位符来格式化字符串的方法。此处,占位符由一个美元符号 ($) 和一个标识符定义。

The String Template class belongs to the string module and provides a way to format strings by using placeholders. Here, placeholders are defined by a dollar sign ($) followed by an identifier.

Example

以下示例显示如何使用模板类设置字符串格式。

The following example shows how to use Template class to format strings.

from string import Template

# Defining template string
str = "Hello and Welcome to $name !"

# Creating Template object
templateObj = Template(str)

# now provide values
new_str = templateObj.substitute(name="Tutorialspoint")
print(new_str)

它将生成以下 output

It will produce the following output

Hello and Welcome to Tutorialspoint !