(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.
Re: (Python) Variables inside strings
Quote:
Originally Posted by
durand
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)
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')
Re: (Python) Variables inside strings
Awesome. I'll try both ways! Thanks guys.
Re: (Python) Variables inside strings
Is there a way to do this with numerical values and not just text based numbers?
Re: (Python) Variables inside strings
That should work with numericals as well. How do you want it to work?
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`
Quote:
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)
Re: (Python) Variables inside strings
Quote:
Originally Posted by
ki4jgt
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
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.
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.