/ Published in: Python
Python allows for strings to be formatted using a predefined format string with a list of variables.
The following is an example of using multiple format strings to display the same data:
The following is an example of using multiple format strings to display the same data:
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
>>>list = ["Brad", "Dayley", "Python Phrasebook", 2006] >>>letter = """ >>>Dear Mr. %s,\n >>>Thank you for your %s book submission. >>>You should be hearing from us in %d.""" >>>display = """ >>>Title: %s >>>Author: %s, %s >>>Date: %d""" >>>record = "%s|%s|%s|%08d" >>>print letter % (list[1], list[2], list[3]) Dear Mr. Dayley, Thank you for your Python Phrasebook book submission. You should be hearing from us in 2006. >>>print display % (list[2], list[1], list[0], list[3]) Title: Python Phrasebook Author: Dayley, Brad Date: 2006 >>>print record % (list[0], list[1], list[2], list[3]) Brad|Dayley|Python Phrasebook|00002006