Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

keketsu

  • 4 years ago

I've been using this older c-style substitution in Python: "Hello, %s!" % name Apparently there is a newer way of substituting variables. Can someone fill me in on this? Thanks.

  • This Question is Closed
  1. bmp
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 4

    The other way to format that I know of is to create a dictionary, i.e.: print 'This %(name)s is wrong by %(#)d' % {'name' : 'sentence', '#': 4}. I do think that Python 3.x has another way to format, but I don't really remember it. I will look into it :-)

  2. bmp
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 4

    Ah, found it. According to the BDFL's Introduction to Python, one should use str.format because of deprecation. Check this example: http://codepad.org/GSOQwMB8

  3. keketsu
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Great! Thanks, bmp.

  4. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy