Advertisement
python_notes

String formatting: % vs. .format

Apr 19th, 2015
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.42 KB | None | 0 0
  1. # Link:  http://stackoverflow.com/questions/5082452/python-string-formatting-vs-format
  2.  
  3.  
  4. #!/usr/bin/python
  5.  
  6. sub1 = "python string!"
  7. sub2 = "an arg"
  8.  
  9. a = "i am a %s" % sub1
  10. b = "i am a {0}".format(sub1)
  11.  
  12. c = "with %(kwarg)s!" % {'kwarg':sub2}
  13. d = "with {kwarg}!".format(kwarg=sub2)
  14.  
  15. print a    # "i am a python string!"
  16. print b    # "i am a python string!"
  17. print c    # "with an arg!"
  18. print d    # "with an arg!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement