Python string addition and string formatting

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.