有多种方法可以连接字符串。对于连接,您可以使用传统的 + 运算符或 Python Join,或者使用逗号分隔字符串文本。在本节中,我们将讨论如何通过示例进行字符串连接。
使用 + 运算符进行 Python 字符串连接
这是字符串连接的基本示例,我们使用 + 运算符连接两个 a 和 b。
如果您在单个字符串对象上使用 * 运算符以及数值,那么该句子将重复给定次数并返回多个单词。
str1 = 'a' + 'b' print(str1) str2 = 'Hi' * 2 print(str2) str3 = 'Hi' * 4 print(str3)
使用算术运算符 + 连接字符串并返回新输出
ab
HiHi
HiHiHiHi
在进行连接时,您应该注意两个字符串变量之间的分隔符。
Python 不提供任何分隔符或定界符。因此,在添加多个字符串时,您必须手动添加额外的空格。在最后一个语句中,我们使用了 += 运算符,它会将 str2 添加到 str1 并更新原始字符串。
str1 = 'Tutorial' str2 = 'Gateway' str3 = str1 + str2 print(str3) print() str4 = str1 + ' ' + str2 print(str4) print() str5 = str1 + ', ' + str2 print(str5) str1 += str2 print(str1)
TutorialGateway
Tutorial Gateway
Tutorial, Gateway
Tutorial Gateway
使用逗号进行 Python 字符串连接
将单词与用逗号分隔的 print 语句 放在一起将合并句子。例如,此示例中的第一个 print 语句连接 str1 和 str2。
str1 = 'Hello '
str2 = 'World '
str3 = 'Tutorial '
str4 = 'Gateway'
print('\nAfter = ', str1, str2)
print()
print('After = ', str1, str2, str3)
print()
print('After = ', str1, str2, str3, str4)

使用 format 进行 Python 字符串连接
它有一个 format 函数。您可以将此 format 函数与大括号一起使用。在这里,{} 表示句子或单词。您可以在 {} {} 之间使用任何分隔符,例如空格或逗号。
str1 = 'Tutorial'
str2 = 'Gateway'
c1 = '{}{}'.format(str1, str2)
print(c1)
print()
c2 = '{} {}'.format(str1, str2)
print(c2)
print()
c3 = '{} - {}'.format(str1, str2)
print(c3)
print()
c4 = '{a} **+** {b}'.format(a = str1, b = str2)
print(c4)
TutorialGateway
Tutorial Gateway
Tutorial - Gateway
Tutorial **+** Gateway
在此编程语言中,您可以使用 %s 格式选项来连接字符串,因此我们使用此 % 符号连接两个或更多字符串。
str1 = 'Tutorial' str2 = 'Gateway' str3 = 'Python' year = 2018 c1 = '%s%s' %(str1, str2) print(c1) print() c2 = '%s %s %s' %(str3, str1, str2) print(c2) print() c3 = 'Learn %s at %s %s - Year %d' %(str3, str1, str2, year) print(c3) print() c4 = '%s %s %s %s' %(str1, str2, str3, year) print(c4)
使用 %s 格式输出合并
TutorialGateway
Python Tutorial Gateway
Learn Python at Tutorial Gateway - Year 2018
Tutorial Gateway Python 2018
使用 str 进行 Python 字符串和整数连接
与大多数编程语言不同,它不会隐式地将整数转换为字符串以执行连接。如您所见,它正在抛出错误 TypeError: can only concatenate str (not "int") to str
text = 'Tutorial Gateway' year = 2018 print(text + year)
Traceback (most recent call last):
File "/Users/suresh/Desktop/simple.py", line 4, in <module>
print(text + year)
TypeError: can only on str (not "int") to str
要解决此问题,您必须使用 str 函数将 int 值 year 转换为 str 字符串。
text = 'Tutorial Gateway ' year = 2018 print(text + str(year))
Tutorial Gateway 2018
让我在一个程序中使用所有可用选项。
text = 'Hello World '
year = 2018
print(text + str(year))
print()
print('{}{}'.format(text, year))
print()
print('%s%s' %(text, year))
print()
print(text, year)
print()
print(f'{text}{year}')
Hello World 2018
Hello World 2018
Hello World 2018
Hello World 2018
Hello World 2018
此示例与上述相同。但是,这次我们允许用户输入自己的句子来执行此操作。
str1 = input('Please enter the First : ')
str2 = input('Please enter the Second : ')
a = str1 + str2
print(a)
print()
b = str1 + ' ' + str2
print(b)
print()
print('{} {}'.format(str1, str2))
print()
print('%s %s' %(str1, str2))
print()
print(str1, str2)
Please enter the First : Java
Please enter the Second : Tutorial
JavaTutorial
Java Tutorial
Java Tutorial
Java Tutorial
Java Tutorial
使用 join 进行 Python 字符串连接
我们有 Join 函数来连接两个或更多句子。您可以使用此函数组合单词列表。' '.join(str_list) 将 str_list 中的所有单词用空格分隔。
str_list = ['Apple', 'Orange', 'Mango', 'Kiwi', 'Cherry'] a = ' '.join(str_list) print(a) print() b = ', '.join(str_list) print(b) print() c = ' $ '.join(str_list) print(c)
Apple Orange Mango Kiwi Cherry
Apple, Orange, Mango, Kiwi, Cherry
Apple $ Orange $ Mango $ Kiwi $ Cherry
您不必使用列表来使用此 Join 函数。您可以在连接之前将不同的单词列表转换为实际列表。
s1 = 'Hello ' s2 = 'World ' s3 = 'Welcome ' s4 = 'Guys' co1 = ' '.join([s1, s2]) print(co1) print() co2 = ', '.join([s1, s2, s3]) print(co2) print() co3 = ' *+* '.join([s1, s2, s3, s4]) print(co3)
Hello World
Hello , World , Welcome
Hello *+* World *+* Welcome *+* Guys