git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

String slices


On 2019-08-10 09:10:12 +1000, Cameron Simpson wrote:
> On 09Aug2019 22:28, Paul St George <email at paulstgeorge.com> wrote:
> > On 09/08/2019 16:29, Rhodri James wrote:
> > > (Actually I would probably use outstream.write() and do my own
> > > formatting, but let's not get side-tracked ;-)
> > > 
> > I would love to hear your outstream.write() side-track!
> 
> I am not Rhodri James, but you can just write strings to text files:
> 
>  outstream.write("X: ")  # note, includes the space separator
>  outstream.write(str(thing[0]))
>  outstring.write("\n")

You can also format the string before passing it to write (which is
probably what Rhodri meant by "do my own formatting", like this:

    outstream.write("X: %7.2f\n" % thing[0])

or this

    outstream.write("X: {0:7.2f}\n".format(thing[0]))

or (since Python 3.6) this:

    outstream.write(f"X: {thing[0]:7.2f}\n")

There are of course many variants to all three methods.

        hp

-- 
   _  | Peter J. Holzer    | we build much bigger, better disasters now
|_|_) |                    | because we have much more sophisticated
| |   | hjp at hjp.at         | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20190810/c90cbeec/attachment.sig>