Home  >  Article  >  Backend Development  >  String concatenation in python

String concatenation in python

巴扎黑
巴扎黑Original
2016-12-08 09:41:481415browse

There are many string connection methods in python, to summarize:
1 The most original string connection method: str1 + str22 python new string connection syntax: str1, str23 Strange string method: str1 str24 % connection string: ' name:%s; sex: ' % ('tom', 'male')5 String list connection: str.join(some_list)
The first method is probably known to anyone with programming experience, just use it directly "+" to connect two strings:
'Jim' + 'Green' = 'JimGreen'
The second one is more special. If the two strings are separated by "comma", then the two strings will be concatenated. , however, there will be an extra space between the strings:
'Jim', 'Green' = 'Jim Green'
The third type is also unique to Python, just put the two strings together, there is a space or No blanks: two strings are automatically concatenated into one string:
'Jim''Green' = 'JimGreen' 'Jim' 'Green' = 'JimGreen'
The fourth function is more powerful and draws on the printf function in C language Function, if you have a C language foundation, just read the documentation to find out. 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:
'%s, %s' % ('Jim', ' Green') = 'Jim, Green'
The fifth technique is to use the string function join. This function accepts a list and then connects each element in the list with a string:
var_list = ['tom', 'david', 'john']
a = '###' a.join(var_list) = ' tom###david###john'

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn