View Full Version : (Python) Variables inside strings
durand
May 8th, 2008, 01:23 PM
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.
<smiley shortcut=':)' checksum='d224f5890bad7f01e766cbcb9184d4703b65f413 ' filename='smile.png'/>
...where the values for shortcut, checksum and filename are variables in my python script.
Thanks.
LaRoza
May 8th, 2008, 01:37 PM
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:
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.
output = '<smiley shortcut="%s" checksum="%s" filename="%s"/>' % (shortcut,checksum,filename)
pmasiar
May 8th, 2008, 02:13 PM
or, more flexible way:
print "my name is %(name)s and I like %(pet)s" % dict(name='Joe', pet='platypus')
durand
May 8th, 2008, 02:23 PM
Awesome. I'll try both ways! Thanks guys.
ki4jgt
April 6th, 2011, 07:48 PM
Is there a way to do this with numerical values and not just text based numbers?
durand
April 6th, 2011, 08:46 PM
That should work with numericals as well. How do you want it to work?
ki4jgt
April 6th, 2011, 11:47 PM
I keep trying this.
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)
Arndt
April 7th, 2011, 04:22 AM
I keep trying this.
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/stdtypes.html#string-formatting-operations
durand
April 7th, 2011, 08:22 AM
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.
andrew1992
April 7th, 2011, 09:17 AM
Actually those don't need to be used anymore - I find this much easier:
print "Hello, {0}! I'm {1} years old".format("John", 18)
Hello, John! I'm 18 years old.
nvteighen
April 7th, 2011, 04:26 PM
Actually those don't need to be used anymore - I find this much easier:
print "Hello, {0}! I'm {1} years old".format("John", 18)
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.
durand
April 7th, 2011, 04:33 PM
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.
Arndt
April 7th, 2011, 04:47 PM
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?
durand
April 7th, 2011, 04:51 PM
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?
wmcbrine
April 7th, 2011, 04:51 PM
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...
nvteighen
April 8th, 2011, 09:37 AM
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.
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.