I have summarized five methods for connecting Python strings: The first method for adding a plus sign (1) and those with programming experience. it is estimated that many languages use a plus sign to connect two strings, python also summarizes five methods for Python string connection:
1. plus sign
First, people with programming experience may know that many languages use the plus sign to connect two strings. in Python, the plus sign is also used to directly connect two strings;
print 'Python' + 'Tab'
Result:
PythonTab
2. comma
The second type is special. use commas to connect two strings. if the two strings are separated by commas, the two strings will be connected, but there will be an extra space between the strings;
print 'Python','Tab'
Result:
Python Tab
3. direct connection
The third type is exclusive to ython. as long as the two strings are put together and there is blank or no blank in the middle, the two strings will be automatically connected to a string;
print 'Python''Tab'
Result:
PythonTab
print 'Python' 'Tab'
Result:
PythonTab
4. Format
The fourth feature is quite powerful. I have used the printf function in C language for reference. if you have the C language basics, read the documentation. In this way, use the symbol "%" to connect a string and a group of variables. the special mark in the string will be automatically replaced with the variable in the variable group on the right:
print '%s %s'%('Python', 'Tab')
Result:
Python Tab
Fifth join
It is a skill to use the string function join. This function accepts a list and uses strings to connect each element in the list in sequence:
str_list = ['Python', 'Tab']a = ''print a.join(str_list)
Result:
PythonTab
The above is a detailed description of the five methods to connect using Python strings. For more information, see other related articles in the first PHP community!