Seven Methods of python String Splicing and Their Performance Comparison

  • 2021-10-24 23:13:38
  • OfStack

python3. x splicing string 1 generally has the following methods:

1. Splice directly through the (+) operator


s = 'Hello'+' '+'World'+'!'
print(s)

Output:

Hello World!

The operation efficiency of string concatenation in this way is inefficient, because when + is used to splice two strings in python, a new string will be generated, and memory needs to be re-applied to generate a new string, which will naturally affect the efficiency when more strings are spliced.

2. Splice through the str. join () method


strlist=['Hello',' ','World','!']
print(''.join(strlist))

Output:

Hello World!

This method is commonly used when converting a collection to a string, ''. join () where'' can be a null character or any other character, and when it is any other character, the string in the collection will be separated by that character, for example:


strlist=['Hello',' ','World','!']
print(','.join(strlist))

Output:

Hello, ,World,!

3. Splice through the str. format () method


s='{} {}!'.format('Hello','World')
print(s)

Output:

Hello World!

To splice strings in this way, it is necessary to pay attention to the fact that the number of {} in the string should be 1 with the number of format method parameters, otherwise an error will be reported.

4. Splice through the (%) operator


s = '%s %s!' % ('Hello', 'World')
print(s)

Output:

Hello World!

This way is basically the same as the way str. format () is used.

5. Splice multiple lines through ()


s = (
  'Hello'
  ' '
  'World'
  '!'
)
print(s)

Output:

Hello World!

python automatically splices multiple lines into one when it encounters unclosed brackets.

6. Splice through the Template object in the string module


from string import Template
s = Template('${s1} ${s2}!') 
print(s.safe_substitute(s1='Hello',s2='World'))

Output:

Hello World!

Template is implemented by first initializing a string with Template. These strings contain one key. By calling substitute or safe_subsititute, the value of key is mapped to the parameter passed in the method, thus importing the string at the specified position. The advantage of this approach is that there is no need to worry about throwing exceptions when the parameter is not 1, such as:


from string import Template
s = Template('${s1} ${s2} ${s3}!') 
print(s.safe_substitute(s1='Hello',s2='World'))

Output:

Hello World ${s3}!

7. Splicing through F-strings

In python version 3.6. 2, PEP 498 proposes a new string formatting mechanism called "string interpolation" or more commonly called F-strings, and F-strings provides a clear and convenient way to embed python expressions into strings for formatting:


s1='Hello'
s2='World'
print(f'{s1} {s2}!')

Output:

Hello World!

We can also execute functions in F-strings:


def power(x):
  return x*x
x=4
print(f'{x} * {x} = {power(x)}')

Output:

4 * 4 = 16

Moreover, F-strings runs very fast, much faster than the two formatting methods%-string and str. format ().

This article mainly explains about python string splicing 7 methods and performance comparison is basically these, more about python string knowledge please see the following links


Related articles: