print format for string ?

i tried to have the first var as a string variable

like this with the %s but it does not work

how can this be done if possible

print (" Color %s D2= %.2f Cmils = %.2f TSC2= %.2f TSC1=%.2f " %(color2,D2,t2,t1))

thanks

if i count the % for substitution,
there are 5
but only 4 vars?
Next, if you want a string-variable and you are getting an error,
the conversion may fail (may be missing).
There is an error if you try to insert a string with %d …
and you did not note what error-message you got!
(the last makes it a riddle … )

no i did get a strange error and it was not for the first argument for a string or count of vars!

but i should have calculated the numbers of parameters there is one extra

i was not certain about this %s for string but seems to be workling fine now!
i could not lcoate this %s in the python examples for format !

go it working this morning !

another problem is how do you align numbers on columns with precision
with no overflow
did not see much examples on this in python doc!

thanks

As well as decimals, you can set the width and padding variable in the format


name="Ricky"
posts=11800
print("%-15s posts:%015d"%(name,posts))

ok … last one link,
havent looked up one for python3.2,
but there should be no changes at this …

the list with options …
> http://docs.python.org/library/stdtypes.html#string-formatting

and the problem for me is, i always have to lookup this in the internet
for a link because i have those docs installed locale with my python-versions
(and some more …)

there are so many ways to play with control characters for format
it’s difficult to understand all the different ways to do things!

here what i would like to get

% d does not work well either too many zeros in front

like to see a list like this

never really seen anything like that before in the examples given in python for format
columns control but no padding with zero in front or back
but align on the dot per column

i have to upload a pic to show it here or the format would have been changed

note :
i already have this page for format on my notes file for python
and there are many examples but does not cover all of them
there are so many it is understandable i guess!

and format is powerfull but a little complicated to get used to and remember if you don’t use it everyday!

thanks guys

fire up the python3.2 shell:

python3.2
and try this:


>>> print("%d %7.2f %9.1f"%(3, 4.58, 5.321))
3    4.58       5.3
>>>

and the last formating number was left for your exercise …

edit: !! the output shown above is not the output of the python example,
this web-forum strips away the blanks/spaces …
(ok … now with “code” part it displays it …)