How Python strings are concatenated
This article mainly introduces how to concatenate Python strings, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
Plus sign
If you have experience in developing other programming languages, you must know that in many languages, two strings are concatenated with a plus sign. In Python programming language, two strings can also be concatenated directly with "+".
Print 'Python' +' Tab'
Results:
PythonTab
Comma
Concatenate two strings with a comma. If two strings are separated by a comma, the two strings will be concatenated, but there will be an extra space between the strings.
Print 'Python','Tab'
Results:
Python Tab
Direct connection
This method is unique to Python. As long as you put two strings together with or without white space, the two strings will be automatically concatenated into one string.
Print 'Python''Tab'
Results:
PythonTab
Formatting
This way is more powerful, drawing lessons from the function of the printf function in C language, if you have a C language foundation, take a look at the document to know. In this way, a string and a set of variables are concatenated with the symbol "%", and the special token in the string is automatically replaced with the variable in the variable group on the right:
Print's% slots% ('Python',' Tab')
Results:
Python Tab
Join () function
It's a trick, using the function join of the string. This function takes a list and concatenates each element of the list with a string in turn:
Str_list = ['Python',' Tab']
A =''
Print a.join (str_list)
Results:
PythonTab
Thank you for reading this article carefully. I hope the article "how to concatenate Python strings" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!