Display Python strings

Source: Internet
Author: User
Make a summary:

Maybe I haven't found a better method yet, but unfortunately, Python does not seem to support similar shell scripts, Perl The scalar interpolation is supported, so it cannot be displayed like the following. This kind of personal feeling is the clearest and convenient method.

For example, if a user loses two variables: 'bucketball ', 'bucketing', shell, or per, the following shows I love bucketball and specify the best. # Shellinput = 'basebucketball 'input2 = 'shortming' print' I love $ input and $ input2 the best' # Perl $ input = 'bucketball '$ input2 = 'shortming' print' I love $ input and $ input2 the best 'so how can I display it in Python, there are the following methods: select as needed. I hope to directly support the display method similar to the escape script in the future, and regard $ as an escape character :) 1 Import Sys
2 Input = SYS. argv [ 1 ]
3 Input2 = SYS. argv [ 2 ]
4
5 S =   ' I love '   + Input +   ' And '   + Input2 +   ' The best '
6 Print (S)
7
8 S =   ' I love % s and % s the best ' % (Input, input2)
9 Print (S)
10
11 Params = { ' Input ' : Input, ' Input2 ' : Input2}
12 S =   ' I love % (input) s and % (input2) s the best ' % Params
13 Print (S)
14
15
16 From String Import Template
17 S = Template ( ' I love $ input and $ input2 the best ' )
18 S = S. substitute (input = Input, input2 = Input2)
19 Print (S)

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.