Python 简明教程

Python - String Concatenation

Concatenate Strings in Python

String concatenation 在 Python 中是连接两个或多个字符串的操作。此操作的结果将是一个新字符串,该字符串包含原始字符串。下图显示了常规字符串连接操作:

String concatenation in Python is the operation of joining two or more strings together. The result of this operation will be a new string that contains the original strings. The diagram below shows a general string concatenation operation −

string concatenation

在 Python 中,连接字符串有很多方法。我们将讨论以下方法:

In Python, there are numerous ways to concatenate strings. We are going to discuss the following −

  1. Using '+' operator

  2. Concatenating String with space

  3. Using multiplication operator

  4. Using '+' and '*' operators together

String Concatenation using '+' operator

"" 运算符众所周知是一个加法运算符,用于返回两个数字的和。然而,它符号在 Python 中充当字符串 concatenation operator 。它处理两个字符串操作数,生成它们的连接。

The "" operator is well-known as an addition operator, returning the sum of two numbers. However, the "" symbol acts as string concatenation operator in Python. It works with two string operands, and results in the concatenation of the two.

在加号符号右侧的 string 的字符附加到其左侧的字符串中。连接的结果是一个新字符串。

The characters of the string on the right of plus symbol are appended to the string on its left. Result of concatenation is a new string.

Example

以下示例演示了使用 + 运算符在 Python 中执行字符串连接操作。

The following example shows string concatenation operation in Python using + operator.

str1="Hello"
str2="World"
print ("String 1:",str1)
print ("String 2:",str2)
str3=str1+str2
print("String 3:",str3)

它将生成以下 output

It will produce the following output

String 1: Hello
String 2: World
String 3: HelloWorld

Concatenating String with space

若要在两个字符串之间插入空格,我们可以使用第三个空字符串。

To insert a whitespace between two strings, we can use a third empty string.

Example

在下例中,我们在连接时在两个字符串之间插入空格。

In the below example, we are inserting space between two strings while concatenation.

str1="Hello"
str2="World"
blank=" "
print ("String 1:",str1)
print ("String 2:",str2)
str3=str1+blank+str2
print("String 3:",str3)

它将生成以下 output

It will produce the following output

String 1: Hello
String 2: World
String 3: Hello World

String Concatenation By Multiplying

另一个我们通常用于两个数字相乘的符号 ,也可以与字符串运算符一起使用。在此, 在 Python 中充当重复运算符。其中一个运算符必须是整数,另一个是字符串。整数运算符指定连接的字符串运算符副本数。

Another symbol *, which we normally use for multiplication of two numbers, can also be used with string operands. Here, * acts as a repetition operator in Python. One of the operands must be an integer, and the second a string. The integer operand specifies the number of copies of the string operand to be concatenated.

Example

在此示例中,* 运算符会连接字符串的多个副本。

In this example, the * operator concatenates multiple copies of the string.

newString = "Hello" * 3
print(newString)

以上代码将生成以下内容 output

The above code will produce the following output

HelloHelloHello

String Concatenation With '+' and '*' Operators

重复运算符 ( ) and the concatenation operator (), can be used in a single expression to concatenate strings. The " “运算符的优先级高于””运算符。

Both the repetition operator () and the concatenation operator (), can be used in a single expression to concatenate strings. The "" operator has a higher precedence over the "" operator.

Example

在下例中,我们一起使用 + 和 * 运算符来连接字符串。

In the below example, we are concatenating strings using the + and * operator together.

str1="Hello"
str2="World"
print ("String 1:",str1)
print ("String 2:",str2)
str3=str1+str2*3
print("String 3:",str3)
str4=(str1+str2)*3
print ("String 4:", str4)

若要形成 str3 字符串,Python 会先连接 3 个 World 副本,然后将结果附加到 Hello

To form str3 string, Python concatenates 3 copies of World first, and then appends the result to Hello

String 3: HelloWorldWorldWorld

在第二种情况下,字符串 str1 和 str2 位于括号中,因此它们的连接首先发生。然后将结果复制三次。

In the second case, the strings str1 and str2 are inside parentheses, hence their concatenation takes place first. Its result is then replicated three times.

String 4: HelloWorldHelloWorldHelloWorld

除了 + 和 * 之外,不能将其他算术运算符与字符串运算符一起使用。

Apart from + and *, no other arithmetic operators can be used with string operands.