[pythonvis] Re: Formatted strings.
- From: "Richard Dinger" <rrdinger@xxxxxxxxxx>
- To: <pythonvis@xxxxxxxxxxxxx>
- Date: Fri, 7 Jul 2017 06:57:03 -0700
This will take a look at how to control and format your text output. You will
learn how to set the width of data, set the precision of floating point numeric
data, left justify data and other formatting options.
Python's string formatting process uses a format definition string to control
the format of the output. The format definition string is a normal single or
double quoted string, but there are data conversion targets embedded in the
string. The format definition string is a sort of template string of your
output.
The modulo operator (%) is used as the operator joining the format string on
the left side with the arguments on the right side. And the % character is
also used to mark the conversion targets
The overall syntax looks like:
'format string' % (arguments)
The arguments are applied to the format string to produce the output string.
For example using the simple int conversion %d to demonstrate the basic idea:
'Val1: %d and val2: %d' % (-7, 15)
'Val1: -7 and val2: 15' % (-7, 15)
The format definition string is reproduced exactly except for the format
conversions targets indicated by %d. Each format conversion target is replaced
with the next argument value. The %d specifies the default conversion to
integer format. Note the arguments must be enclosed in parentheses if more
than one argument.
Types of Conversion
there are many conversion type codes and they all can be found in the standard
documentation. For this Slice of Py article we will look in detail at the main
types of data conversions; string (s) integer (d) and floating point (f).
We will not cover the others here other than to mention there are codes for
conversion to octal (o), hex (x), capital hex (X), exponential floating point
(e) and others. They all follow the same general rules so you can usually
guess how to use any code.
Default Conversions
The % sign and the type code will get you the default behavior for the code.
We can take a look and see what that default is. Note I include the characters
and < on either side of the conversion specification so we can see exactly
what is in the output:
'int vals >%d< >%d< >%d<' % (5, 50, -50)
int vals >5< >50< >-50<
The default d target expands to fit the number of digits and a sign if negative.
Now try some floats:
'float vals >%f< >%f< >%f<' % (.42, -42.42, -42000000.0)
float vals >0.420000< >-42.420000< >-42000000.000000<'
The default f target has six digits after the decimal and also expands to fit
the size of the number, but there is effectively a minimum because of the six
digits after the decimal point.
And the default s target simply inserts a string into the format string:
'str vals >%s< >%s<' % ('x', 'The quick red fox')
'str vals >x< >The quick red fox<'
Adding More Detail
The complete format conversion system is quite flexible. The detailed syntax
for a format conversion target is:
%[flags][width[].precision]code
where items enclosed by [] are optional
width is minimum field width
.precision is number of digits after decimal
flags are:
- for left justify
+ for include sign for all
0 zero fill left side
Try some examples:
'>%5d< >%+5d< >%05d<' % (5, 5, 5)
'> 5< > +5< >00005<'
'>%5.2f< >%5.1f< >%5.0f<' % (3.154, 3.154, 3.154)
'> 3.15< > 3.2< > 3<'
Now you can produce output records of almost any form including fixed length
records with zero left fill for numbers.
The string formatting expression can also be used as yet another string
processing tool. Strings that are taken apart with split sometimes are easier
to reassemble with a format expression. For example changing a title that
starts with 'The' to a title that ends with ', The' can be done in two lines:
first, rest = title.split(' ', 1) # split on first space
title = '%s, %s' % (rest, first)
The above two lines would change 'The Four Seasons' to 'Four Seasons, The' and
make it more suitable for sorting.
Richard
From: Gabriele Battaglia (Redacted sender "iz4apu" for DMARC)
Sent: Friday, July 07, 2017 5:22 AM
To: pythonvis@xxxxxxxxxxxxx
Subject: [pythonvis] Formatted strings.
Hi all, once again.
Thanks for info on set data type. Now I must discover what are the operators
effect the sets kind.
Example: how to compare 2 sets in order to highlight the common items?
But my today’s question is different.
I’m trying to migrate my set of scripts from Python 2.7 to 3.6. Doing that, I
discovered a new, fascinating way to format the strings and I would like to
know it better.
It is something like that:
n=‘Gabriel’
f’Hello {n}’
‘Hello Gabriel’
It’s very useful.
My question is: there is only this way to include variables in a formatted
string?
Is there a way to obtain a desired layout also? Let’s say we have a float to
include but we want to show just it’s first two digits, like 3.14
Can I do it using the f”” string?
Thanks.
Gabriele.
—
Namasté!
Sent from my iMac27. (Libero)
Other related posts: