Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: (Python) Variables inside strings

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

    Question (Python) Variables inside strings

    Is it possible in python to print a string with variables embedded inside it. I'm new to python so I'm not even sure how you would show that some word is a variable except when they are separate.

    Ex:

    print "Hello var1. You are in var2, etc."


    I've seen print " "hello %s" % name " being used but I'm not sure how to add more text and variables.

    My actual problem is that I need to print this line into a file.

    Code:
    <smiley shortcut=':)' checksum='d224f5890bad7f01e766cbcb9184d4703b65f413' filename='smile.png'/>
    ...where the values for shortcut, checksum and filename are variables in my python script.

    Thanks.

  2. #2
    Join Date
    Apr 2007
    Beans
    14,781

    Re: (Python) Variables inside strings

    Quote Originally Posted by durand View Post
    Is it possible in python to print a string with variables embedded inside it. I'm new to python so I'm not even sure how you would show that some word is a variable except when they are separate.
    Yes, you can. Example code:

    Example code:
    Code:
    name = raw_input("What is your name? ")
    
    age = raw_input("What is your age? ")
    
    rank = raw_input("How rank are you? ")
    
    if age.isdigit():
        print "Hello %s, you are %i years old and are %s rank" % (name,int(age),rank)
    else:
        print "%s, \"%s\" is not an age!" % (name,age)
    This sort of thing can be done with strings in many situations. It is similar to printf() in C if you know that language.

    Code:
    output = '<smiley shortcut="%s" checksum="%s" filename="%s"/>' % (shortcut,checksum,filename)
    Last edited by LaRoza; May 8th, 2008 at 06:52 PM.

  3. #3
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: (Python) Variables inside strings

    or, more flexible way:

    PHP Code:
    print "my name is %(name)s and I like %(pet)s" dict(name='Joe'pet='platypus'

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

    Re: (Python) Variables inside strings

    Awesome. I'll try both ways! Thanks guys.

  5. #5
    Join Date
    Oct 2007
    Location
    Kentucky, USA
    Beans
    731
    Distro
    Ubuntu

    Re: (Python) Variables inside strings

    Is there a way to do this with numerical values and not just text based numbers?
    Which is more important in obtaining the truth, "what" or "why"? Trick question. They are of equal importance.
    Freely ye have received, freely give.

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

    Re: (Python) Variables inside strings

    That should work with numericals as well. How do you want it to work?

  7. #7
    Join Date
    Oct 2007
    Location
    Kentucky, USA
    Beans
    731
    Distro
    Ubuntu

    Re: (Python) Variables inside strings

    I keep trying this.

    Code:
    v = 2011
    b = v - 1
    
    print """Happy New Years. Good-bye %v
    
    Further comments. . . blah blah blah""" % (v)
    I even went as far as to change v and b to v = `v` and b = `b`
    ValueError: unsupported format character 'v' (0x76) at index 7
    That's what I get EVERY time. I've tried single quotes, double, triple. I've tried changing v and b to their text version (As stated above)
    Last edited by ki4jgt; April 7th, 2011 at 04:51 AM.
    Which is more important in obtaining the truth, "what" or "why"? Trick question. They are of equal importance.
    Freely ye have received, freely give.

  8. #8
    Join Date
    May 2006
    Beans
    1,787

    Re: (Python) Variables inside strings

    Quote Originally Posted by ki4jgt View Post
    I keep trying this.

    Code:
    v = 2011
    b = v - 1
    
    print """Happy New Years. Good-bye %v
    
    Further comments. . . blah blah blah""" % (v)
    I even went as far as to change v and b to v = `v` and b = `b`

    That's what I get EVERY time. I've tried single quotes, double, triple. I've tried changing v and b to their text version (As stated above)

    You need to use one of the existing conversion specifiers, like %s or %d, depending on the type of the value to be inserted, like in the previous examples in this thread. See http://docs.python.org/library/stdty...ing-operations

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

    Re: (Python) Variables inside strings

    Code:
    v = 2011
    b = v - 1
    
    print """Happy New Year %s. Good-bye %s
    
    Further comments. . . blah blah blah"""%(v, b)
    You always use %s and %d which are placeholders and your actual variables go at the end.

  10. #10
    Join Date
    Feb 2011
    Location
    Cambridge, ON, CAN
    Beans
    105
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: (Python) Variables inside strings

    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.

Page 1 of 2 12 LastLast

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
  •