PDA

View Full Version : C# {n} like , in python



McWeirdo
November 27th, 2009, 03:25 PM
Hi , I've encountered a problem in python, as I've learned more advanced stuff and skipped the basics I'm missing a simple thing:
I have 2 variables say x and y and I want to print:
" x:value_of_x y:value_of_y"
where the values are the real values of x and y
in C# I would write:
"x:{0} y:{1}",x,y
can't find in py doc the appropriate syntax,
but I think I once saw it's something like that "x:%d y:%d" %x %y
but it doesn't work,it mumbles aboutאטפקד:P
print '+%d*x^%d' %self.data[self.sed-i] %self.sed-i

where sed is integer and the data[index] is integer THE ERROR:
TypeError: not enough arguments for format string

so if anybody could help me ,I would appreciate it.
Thx in advance!!

MCW

0cton
November 27th, 2009, 04:10 PM
you could just use string concatenation
"X:"+str(x)+"Y:"+str(y)
or that is called print formating
%d stands for decimal %s for string there is also %h for hex and other stuff as well
the way it works in python is
"this is %s and you have %d lives" % ("sparta",98)
you present it with a bunch of % and give it a tuple argument of all required replacements
I think you must have printf in C# were you do
printf("This is %s and you have %d lives %s","sparta",45,"man")

McWeirdo
November 27th, 2009, 04:12 PM
you could just use string concatenation
"X:"+str(x)+"Y:"+str(y)
or that is called print formating
%d stands for decimal %s for string there is also %h for hex and other stuff as well
the way it works in python is
"this is %s and you have %d lives" % ("sparta",98)
you present it with a bunch of % and give it a tuple argument of all required replacements
I think you must have printf in C# were you do
printf("This is %s and you have %d lives %s","sparta",45,"man")

Thank you :D

LKjell
November 27th, 2009, 08:35 PM
The % is deprecated in version 3. If you use v2.6 it is recommended to use the new syntax. http://docs.python.org/library/string.html#formatstrings

wmcbrine
November 27th, 2009, 08:41 PM
Simplest, in this case (for Python 2.x):

print 'x:', x, 'y:', y
or -- one string with formatting codes and a tuple of values:

print 'x: %d y: %d' % (x, y)
2.6 and later add a new style that might seem familiar to you:

print 'x: {0} y: {1}'.format(x, y)