There are many ways to concatenate strings in Python, and here's a concrete summary today:
①. The most primitive string connection method: Str1 + str2
②.python New String Connection syntax: STR1, STR2
③. Strange string Way: str1 str2
④.% connection string: ' name:%s; Sex: '% (' tom ', ' Male ')
⑤. String List connection: Str.join (some_list)
The following specific analysis:
First, presumably as long as the programmer has the experience of the people, it is estimated that the direct use of "+" to connect two strings:
‘Jim‘+ ‘Green‘ = ‘JimGreen‘
The second is special, if two strings are separated by "comma", then the two strings will be concatenated, but there will be a space between the strings:
‘Jim‘, ‘Green‘= ‘Jim Green‘
The third is also Python-specific, as long as the two strings together, the middle of a blank or no blank: two strings are automatically connected to a string:
‘Jim‘‘Green‘= ‘JimGreen‘
‘Jim‘‘Green‘ = ‘JimGreen‘
The fourth function is more powerful, drawing on the function of the printf function in C, if you have a C language basis, look at the document to know. In this way, a string and a set of variables are concatenated with the symbol "%", and the special tags in the string are automatically replaced with the variables in the right variable group:
‘%s, %s‘% (‘Jim‘, ‘Green‘) = ‘Jim, Green‘
The fifth type is the technique, using the string function join. This function takes a list and then connects each element of the list with a string:
var_list =[‘tom‘, ‘david‘, ‘john‘]
a =‘###‘
a.join(var_list) =‘tom###david###john‘
In fact, Python also has a way of string connection, but not much, is the string multiplication, such as:
a =‘abc‘
a *3 = ‘abcabcabc‘
FROM:http://www.jb51.net/article/54106.htm
Python string connection method (GO)