Tips for displaying Python strings

Source: Internet
Author: User

The application of the Python programming language is a good computer programming language for developers. In practice, the application of strings is a basic operation technology. Today, we will give you a detailed introduction to the operations related to Python string display.

Maybe I haven't found a better method yet, but unfortunately, Python does not seem to support scalar interpolation like shell scripts and perl currently, therefore, it is impossible to display such a personal feeling as follows. This is the clearest and most 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.

 
 
  1. #shell  
  2. input = 'basketball' 
  3. input2 = 'swimming' 
  4. print 'I love $input and $input2 the best'  
  5. #perl  
  6. $input = 'basketball' 
  7. $input2 = 'swimming' 
  8. print 'I love $input and $input2 the best' 

How to Implement Python string display? 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. s = 'I love ' + input + ' and ' + input2 + ' the best'  
  5. print(s)  
  6. s = 'I love %s and %s the best'%(input, input2)  
  7. print(s)  
  8. params= {'input': input, 'input2': input2 }  
  9. s = 'I love %(input)s and %(input2)s the best'%params  
  10. 13 print(s)  
  11. from string import Template  
  12. s = Template('I love $input and $input2 the best')  
  13. ss =s.substitute(inputinput=input, input2input2=input2)  
  14. print(s) 

The preceding section describes how to display Python strings.

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.