Python String Concatenation 是编程中非常常见的操作. Python String Concatenation 可以通过各种方式进行。本教程旨在探索 Python 程序中连接字符串的不同方式。
Python String 连接
我们可以使用以下方法进行 string concatenation:
- 使用 + 运算符
- 使用 join() 方法
- 使用 % 运算符
- 使用 format() 函数
- 使用 f-string (字母字符串交叉)
使用 + 操作员的 String Concatenation
这是最简单的链接方式,让我们来看看一个简单的例子。
1s1 = 'Apple'
2s2 = 'Pie'
3s3 = 'Sauce'
4
5s4 = s1 + s2 + s3
6
7print(s4)
输出:‘ApplePieSauce’ 让我们看看另一个例子,我们将从用户输入中获得两个字符串,并将它们连接起来。
1s1 = input('Please enter the first string:\n')
2s2 = input('Please enter the second string:\n')
3
4print('Concatenated String =', s1 + s2)
输出:
1Please enter the first string:
2Hello
3Please enter the second string:
4World
5Concatenated String = HelloWorld
It's very easy to use + operator for string concatenation. However, the arguments must be a string.
1>>>'Hello' + 4
2Traceback (most recent call last):
3 File "<input>", line 1, in
4TypeError: can only concatenate str (not "int") to str
我们可以使用 str() 函数来获取对象的字符串表示。
1print('Hello' + str(4))
2
3class Data:
4 id = 0
5
6 def __init__(self, i):
7 self.id = i
8
9 def __str__(self):
10 return 'Data[' + str(self.id) + ']'
11
12print('Hello ' + str(Data(10)))
输出:
1Hello4
2Hello Data[10]
与 + 运算器最大的问题是,我们不能在字符串之间添加任何分离器或分界器,例如,如果我们必须将你好
和世界
与白空间分离器相连,我们将不得不将其写成你好
+ + `世界
。
使用 join() 函数的 string concatenation
我们可以使用 join() 函数来连接字符串与分离器。当我们有字符串的序列时,例如 list或 tuple的字符串,这很有用。如果您不想要字符串分离器,则使用 join() 函数与空字符串。
1s1 = 'Hello'
2s2 = 'World'
3
4print('Concatenated String using join() =', "".join([s1, s2]))
5
6print('Concatenated String using join() and whitespaces =', " ".join([s1, s2]))
输出:
1Concatenated String using join() = HelloWorld
2Concatenated String using join() and spaces = Hello World
使用 % 运算器的 String Concatenation
我们可以使用 % 运算器进行字符串格式,也可以用于字符串连接,当我们想要连接字符串并执行简单的格式化时,它是有用的。
1s1 = 'Hello'
2s2 = 'World'
3
4s3 = "%s %s" % (s1, s2)
5print('String Concatenation using % Operator =', s3)
6
7s3 = "%s %s from JournalDev - %d" % (s1, s2, 2018)
8print('String Concatenation using % Operator with Formatting =', s3)
输出:
1String Concatenation using % Operator = Hello World
2String Concatenation using % Operator with Formatting = Hello World from JournalDev - 2018
使用格式() 函数的 String Concatenation
我们可以使用 string format() 函数来连接和格式化字符串。
1s1 = 'Hello'
2s2 = 'World'
3
4s3 = "{}-{}".format(s1, s2)
5print('String Concatenation using format() =', s3)
6
7s3 = "{in1} {in2}".format(in1=s1, in2=s2)
8print('String Concatenation using format() =', s3)
输出:
1String Concatenation using format() = Hello-World
2String Concatenation using format() = Hello World
Python String format() 函数非常强大,只用它来连接字符串并不是它的正确用途。
使用 f-string 连接字符串
如果你正在使用Python 3.6+,你也可以使用f-string来连接字符串,这是一个新的方法来格式化字符串,并在 PEP 498 - Literal String Interpolation中引入。
1s1 = 'Hello'
2s2 = 'World'
3
4s3 = f'{s1} {s2}'
5print('String Concatenation using f-string =', s3)
6
7name = 'Pankaj'
8age = 34
9d = Data(10)
10
11print(f'{name} age is {age} and d={d}')
输出:
1String Concatenation using f-string = Hello World
2Pankaj age is 34 and d=Data[10]
Python f-string 比 format() 函数更清洁,更易于编写,当一个对象参数被用作字段替代时,它还会调用 str() 函数。
结论
Python String 格式化可以以多种方式进行。根据您的要求使用它们. 如果您需要与一个分界符串的序列进行连接,然后使用 join() 函数. 如果需要一些格式化也需要连接,然后使用 format() 函数或 f-string. 请注意,f-string 可用于 Python 3.6 或更高版本。
您可以从我们的 GitHub 存储库中查阅完整的 Python 脚本和更多 Python 示例。