Add by ZHJ: For a multi-line string connection, the 6th connection method is convenient and no additional space is added when connecting.
Original: http://www.cnblogs.com/bigtreei/p/7892113.html
1. Plus
First, people with programming experience know that many languages use a plus sign to connect two strings, and Python also uses "+" directly to connect two strings;
print ' Python ' + ' Tab '
Results:
Pythontab
Back to top of 2. Comma
The second is special, use a comma to concatenate two strings, if the two strings separated by a "comma", then the two strings will be concatenated, but there will be a space between the strings;
print ' Python ', ' Tab '
Results:
Python Tab
Back to top of 3. Direct connection
The third is also Python-specific, as long as the two strings together, the middle of a blank or no blank, two strings will be automatically connected to a string;
Example 1:
print ' Python ' Tab '
Results:
Pythontab
Example 2:
print ' Python ' Tab '
Results:
Pythontab
Back to top of 4. Formatting
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:
Print '%s%s '% (' Python ', ' Tab ')
Results:
Python Tab
Back to top of 5. Join
Use a string function join; This function receives a list and then connects each element of the list with a string:
Str_list = [' Python ', ' Tab ']a = ' Print A.join (str_list)
Results:
Pythontab
Back to top of 6. Multi-line string concatenation ()
s = (' select * ' from atable ' where id=888 ') print S, type (s) #输出select *from atablewhere id=888 <type ' str ' >
Python encounters unclosed parentheses, automatically stitching multiple lines into one line, and does not treat line breaks, leading spaces as characters, compared to three quotes and line breaks.
Python string concatenation of 6 ways (GO)