String slices

Rhodri James rhodri at kynesim.co.uk
Mon Aug 12 09:24:50 EDT 2019


On 10/08/2019 10:45, Peter J. Holzer wrote:
> 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.

That is indeed what I meant.  I do enough random dumping of things to 
files for debugging purposes that outstream.write() is second nature to 
me now.  I once even went as far as sys.stdout.write() before sanity 
caught up with me ;-)


-- 
Rhodri James *-* Kynesim Ltd



More information about the Python-list mailing list