[Tutor] font/text in pygame
keridee at jayco.net
Thu Apr 28 02:37:58 CEST 2005
>> def displaybalance():
>> for score, name in mylist:
>> slip = 30 - len(name)
>> slip_amt = slip*" "
>> print "%s%s%s" % (name,slip_amt,score)
>> (I did this with the print command to make sure it would produce what
>> I wanted, three strings for the three sample scores I put in this
>> dummy list).
> Yup, you did that. And actually, what happened was that string formatting
> was first used to create a string, and then this string was sent to the
> print command, which displayed it. Your last line is equivalent to:
> foo = "%s%s%s" % (name,slip_amt,score)
> print foo
> The only "magic" thing you can only do with print is the "print a, b, c"
for score, name in mylist:
name = name.ljust(30)
print "%s%s" % (name,score)
Or, if you prefer...
print "\n".join("%-30s%s"%(name,score) for name,score in mylist)
If you don't have python 2.4
print "\n".join([---]) # with the hyphens replaced with the above
Oh, and there is another cool thing you can do with print.
First off, print uses sys.stdout, so if you change it, print changes where
Second, if you use the syntax print >>output,item0,item1,item2
you can redirect manually where things are printed to. output has to be an
object which has a write method.
a = open("myfile.txt","w")
b = "This is the string I want to print out."
gives you a file in the current directory with the filename "myfile.txt"
with the contents "This is the string I want to print out."
Okay, I'm done again,
More information about the Tutor