Re: Controlling output using print with format string



Paul Watson <pwatson@xxxxxxxxxxxxx> writes:

> It is clear that just using 'print' with variable names is relatively
> uncontrollable. However, I thought that using a format string would
> reign the problem in and give the desired output.
>
> Must I resort to sys.stdout.write() to control output?

No. You can control the output of print to whatever degree you want -
just convert the printed objects to strings by hand before printing
them.

> $ python
> Python 2.4.1 (#1, Jul 19 2005, 14:16:43)
> [GCC 4.0.0 20050519 (Red Hat 4.0.0-8)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> s = 'now is the time'
> >>> for c in s:
> ... print c,
> ...
> n o w i s t h e t i m e
> >>> for c in s:
> ... print "%c" % (c),
> ...
> n o w i s t h e t i m e
> >>> for c in s:
> ... print "%1c" % (c),
> ...
> n o w i s t h e t i m e

On the other hand, you can't keep print from putting a space between
all the objects it prints. So you have to convert all the objects into
a single string before printing that string.

<mike
--
Mike Meyer <mwm@xxxxxxxxx> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
.



Relevant Pages

  • Re: printing on the right side of paper
    ... and print the string as it provided better measurement for this. ... default StringFormat used in MeasureString some padding around the string ... You will also need to consider printer hard margins when printing as 0,0 ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Word Wrap while Printing
    ... To get the # of lines used for the wrapped printing you can use the ... MeasureString method that takes a SizeF argument (#7 of 7 in my Intellisense ... printing in and, of course, use the same StringFormat as the DrawString ... of the string the string would be truncated on output. ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Print current selected record to a report
    ... Ok, I got it printing out the current record ok now, but it won't print out ... Dim stDocName As String ... DoCmd.OpenReport "rptFinancialAssesment",,, strWhere ...
    (microsoft.public.access.formscoding)
  • Re: printing variable problems
    ... Marco van de Voort said ... printing a string that is prefixed with a given number of space ... characters, does not always start at the same position? ...
    (comp.lang.pascal.borland)
  • Re: Probably simple problem with networking
    ... printing over 300 blank characters, ... You may want to truncate your string to the ...
    (microsoft.public.dotnet.languages.csharp)