There are many string connection methods in python. I am writing code today. By the way, I would like to summarize the original string connection method: str1 + str2python new string connection syntax: str1, str2 strange string method: there are many string connection methods in str1str2 python. I am writing code today. By the way, Let's summarize:
The most primitive string connection method: str1 + str2
New python string connection syntax: str1, str2
Strange String method: str1 str2
% Connection string: 'Name: % s; sex: '% ('Tom', 'male ')
String list connection: str. join (some_list)
First, as long as you have programming experience, we all know that you can directly use "+" to connect two strings:
'Jim' + 'Green' = 'JimGreen'
The second type is special. if the two strings are separated by commas, the two strings will be connected. However, there will be an extra space between the strings:
'Jim', 'Green' = 'Jim Green'
The third type is exclusive to python. as long as the two strings are put together, there is blank or no blank in the middle: the two strings are automatically connected to one string:
'Jim''Green' = 'JimGreen''Jim' 'Green' = 'JimGreen'
The fourth feature is quite powerful. I have used the printf function in C language for reference. if you have the C language basics, read the documentation. In this way, use the symbol "%" to connect a string and a group of variables. the special mark in the string will be automatically replaced with the variable in the variable group on the right:
'%s, %s' % ('Jim', 'Green') = 'Jim, Green'
The fifth type is a skill. use the string function join. This function accepts a list and uses strings to connect each element in the list in sequence:
var_list = ['tom', 'david', 'john']a = '###'a.join(var_list) = 'tom###david###john'
In fact, there is also a string connection method in python, but not many are used, that is, string multiplication, such:
a = 'abc'a * 3 = 'abcabcabc'
The preceding section describes several common methods of string connection in python. For more information, see other related articles in the first PHP community!