1, in Python, string a occupies a memory address, string B also occupies a memory address, when the string a+b, will be in the memory space to open a new address to store a+b.
A address a
B Address II
A+b address Three
Therefore, the memory occupies three parts, the memory consumption is very large, so try to use string formatting to do character stitching.
2. String formatting
A. Common placeholders
%d: integer
%f: Floating point
%s: string
%x: Hexadecimal integer
b, where formatted integers and floating-point numbers can also specify whether to fill 0 and integers with decimals:
Print ('%2d-%02d'% (3,1)):'3-01'print('%. 2f' %3.1415926): 3.14
C, with a percent of%
Print ('wohaoshuai%%%s' % ('5'))
3. Format string formatting
I1 ="I am {},age {}, {}". Format ('Cairui', 18,'KK')Print(i1) I am cairui,age18, Kki1="I am {0},age {1}, {0}". Format ('Cairui', 18)Print(i1) I am cairui,age18, Cairuii1="I am {name},age {age}, {name}". Format (name='Cairui', age=18)Print(i1) I am cairui,age18, Cairuii1="I am {: s},age {:d}, {: F}". Format ('Cairui', 18,6.1)Print(i1) I am cairui,age18, 6.100000
Python string addition and string formatting