Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: (Python) Variables inside strings

  1. #11
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: (Python) Variables inside strings

    Quote Originally Posted by andrew1992 View Post
    Actually those don't need to be used anymore - I find this much easier:

    Code:
    print "Hello, {0}! I'm {1} years old".format("John", 18)

    Code:
    Hello, John! I'm 18 years old.
    This was introduced in Python 2.6 and is the only legal way in Python 3.0, which dropped the (weird) way using the % operator. Keep that in mind for the future.

  2. #12
    Join Date
    Nov 2005
    Location
    Leeds, UK
    Beans
    1,634
    Distro
    Ubuntu Development Release

    Re: (Python) Variables inside strings

    Ah, thats much neater than %, which isn't very pythonic...The only thing is that if you plan on sharing your code, others must have python 2.6 or above.

  3. #13
    Join Date
    May 2006
    Beans
    1,787

    Re: (Python) Variables inside strings

    Quote Originally Posted by nvteighen View Post
    This was introduced in Python 2.6 and is the only legal way in Python 3.0, which dropped the (weird) way using the % operator. Keep that in mind for the future.
    Will it be possible to (re)define the % operator in 3.0 to do what it does in 2.7?

  4. #14
    Join Date
    Nov 2005
    Location
    Leeds, UK
    Beans
    1,634
    Distro
    Ubuntu Development Release

    Re: (Python) Variables inside strings

    Doubt it. 3.x isn't meant to be backwards compatibile with 2.x. Also, isn't % also a modulus operator in normal circumstances. Maybe thats what it has become in 3.x?

  5. #15
    Join Date
    Mar 2005
    Beans
    947
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: (Python) Variables inside strings

    Quote Originally Posted by nvteighen View Post
    This was introduced in Python 2.6 and is the only legal way in Python 3.0, which dropped the (weird) way using the % operator.
    No it didn't (hasn't). At least not as of 3.1.2...

  6. #16
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: (Python) Variables inside strings

    Quote Originally Posted by wmcbrine View Post
    No it didn't (hasn't). At least not as of 3.1.2...
    Yup, you're right, I've tried it on my Python 3.1.3. But I'm sure to have read that somewhere; funny enough, I haven't found it again, so I have probably missed something

    I apologize for the misinformation.

  7. #17
    Join Date
    Dec 2012
    Beans
    1

    Re: (Python) Variables inside strings

    Could you not just use the str() statement? Example:

    ticketCost = 10

    print "The cost of your ticket is $" + str(ticketCost) + "."

    Or is that the same thing?

  8. #18
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,297
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: (Python) Variables inside strings

    This works too, it is just very ugly.
    「明後日の夕方には帰ってるからね。」


  9. #19
    Join Date
    May 2008
    Location
    UK
    Beans
    1,447
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: (Python) Variables inside strings

    Quote Originally Posted by Bachstelze View Post
    This works too, it is just very ugly.
    +1
    String formatting is the way to go, a lot more flexibility than using str - since you get all the featurs of the formatting mini-language too.

    Using string formatting (using the {0} type format) has the added benefit of being able to be overwritten for custom classes (you simply define a __format__ method for your class): so you can define what a format string of "+02.8d" actually means for your class, or you could even define your own format mini-langauge for your class.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

Page 2 of 2 FirstFirst 12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •