Python string concatenation of 6 ways:
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;
Results:
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;
Results:
3. Direct connection
The third Kind is also, Ython unique, as long as the two strings together, the middle of a blank or no blank, two strings will be automatically connected to a string;
Results:
Results:
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:
1 |
print ‘%s %s‘ % ( ‘Python‘ , ‘Tab‘ ) |
Results:
The fifth kind of join
It's a technique, a function join using a string. This function takes a list and then connects each element of the list with a string:
123 |
str_list = [ ‘Python‘ , ‘Tab‘ ] a = ‘‘ print a.join(str_list) |
Results:
Sixth multi-line string concatenation ()
s = ('select *'from atable' where id= 888')print s, type (s)# output select * from '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.
6 ways to stitch a python string