N Methods of String Concatenation in Python_Python String Concatenation_Gu Sui’s Blog
class=”markdown_views prism-atom-one-light”> Fromhttps://mp.weixin.qq.com/s/8cPCMoW6pwcH9ENXGtoGJg Python splicing strings generally have the following methods: 1. Splicing directly through the (+) operator s = ‘Hello’+’ ‘+’World’+’!’ print(s) Output result: Hello World! The operation of string concatenation in this way is inefficient, because a new string will be generated when using + to splice two strings in python, and the memory needs to be re-applied to generate a new string. When splicing strings More will naturally affect the efficiency. 2. Splicing by str.join() method strlist=[‘Hello’,’ ‘ ,’World’,’!’] print(”.join(strlist )) Output result: Hello World! This method is generally used to convert a collection into a string. “.join()” can be a null character or any other character. When it is any other character, the string in the collection will be the character separated, for example: strlist=[‘Hello’,’ ‘ ,’World’,’!’] print(‘,’.join( strlist)) Output result: Hello, ,World,! 3. Splicing by str.format() method s='{} {}!’ .format(‘Hello’,’ World’) print(s) Output result: Hello World! When splicing strings in this way, it should be noted that the number of {} in the string must be consistent with the number of format method parameters, otherwise an error will be reported. 4. Splicing by (%) operator s = ‘%s %s!’ % (‘Hello’, ‘World’) print(s) Output result:…