Results 1 to 10 of 19

Thread: (Python) Variables inside strings

Hybrid View

  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?

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
  •