Analysis of Python string format output method

Source: Internet
Author: User
Tags python string format
This article analyzes the Python string format output method. Share to everyone for your reference, as follows:

There are 3 ways we can format a build string:

1-Tuple placeholder

m = ' python ' astr = ' I love%s '% Mprint astr

2 The Format method of the string

m = ' python ' astr = ' I love {python} '. Format (python=m) print Astr

3 Dictionary formatting strings

m = ' python ' astr = ' I love% (python) s '% {' python ': M}print astr

You can choose the appropriate method according to their actual situation, it is recommended to use the format method of the string or the placeholder formatting of the dictionary, because it will not be affected by the location of the parameter, only need the parameter name is the same line.

More readers interested in Python related content can view the topic: Python string manipulation tips Summary, Python Introductory and Advanced classic tutorials, and Python file and directory Operations Tips Summary

I hope this article is helpful for Python program design.

  • 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: 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.