There are many ways to connect strings in Python, and here's a concrete summary today:
①. The original 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 of all, presumably as long as people with programming experience, 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 commas, the two strings will be concatenated, but there is one more space between the strings:
' Jim ', ' green ' = ' Jim Green '
The third is also unique to Python, as long as you put two strings together, there is a gap in the middle or no blank: two strings are automatically connected to a string:
' jim ' green ' = ' jimgreen '
jim ' green ' = ' jimgreen '
The fourth kind of function is quite powerful, borrowed from the C language function of printf, if you have the base of C language, see the document to know. In this way a string and a set of variables are concatenated with the symbol "%", and special tags in the string are automatically replaced with the variables in the right variable group:
'%s '% (' Jim ', ' green ') = ' jim, green '
The fifth kind of technique is the use of string function join. This function takes a list and then joins each element of the list in a string:
Var_list = [' Tom ', ' David ', ' John ']
a = ' ### '
a.join (var_list) = ' tom## #david # # #john '
In fact, Python also has a string concatenation method, but not much, is string multiplication, such as:
A = ' abc '
A * 3 = ' abcabcabc '
I hope the example described in this article will help you with your Python programming.