Summarized the 5 methods of Python string connection:
Related recommendations: "python video"
plus sign
The first one, people with programming experience probably know that in many languages, the plus sign is used to connect two strings. The same is true in Python. You can directly use " " to connect two strings. ;
print 'Python' + ‘Tab’
Result:
PythonTab
Comma
The second one is more special, use comma to connect two strings, if the two strings use "comma" separated, then the two strings will be concatenated, but there will be an extra space between the strings;
print 'Python','Tab’
Result:
Python Tab
Direct connection
The third type is also unique to Python. As long as two strings are put together, with or without blanks in the middle, the two strings will automatically be concatenated into one string;
print 'Python''Tab’
Result:
PythonTab
print 'Python' 'Tab’
Result:
PythonTab
Formatting
The fourth function is more powerful and draws on C language If you have a C language foundation, you will know the functions of the printf function in the documentation. This method uses the symbol "%" to connect a string and a group of variables. The special marks in the string will be automatically replaced with the variables in the variable group on the right:
print '%s %s'%('Python', 'Tab')
Result:
Python Tab
join
is a skill, using the string function join. This function accepts a list and then concatenates each element in the list with a string:
str_list = ['Python', 'Tab'] a = ''print a.join(str_list)
Result:
PythonTab
The above is the detailed content of How to concatenate python strings. For more information, please follow other related articles on the PHP Chinese website!