Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #27422
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Subject | Re: How does .rjust() work and why it places characters relative to previous one, not to first character - placed most to left - or to left side of screen? |
| Date | 2012-08-19 14:56 -0400 |
| Organization | > Bestiaria Support Staff < |
| References | <db49919e-9c9f-4e1f-8dfe-2765c6717dbe@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3523.1345402599.4697.python-list@python.org> (permalink) |
On Sun, 19 Aug 2012 09:25:03 -0700 (PDT), crispy <ryniek90@gmail.com>
declaimed the following in gmane.comp.python.general:
<snip>
> So I have guessed, that characters processed by .rjust() function, are placed in output, relative to previous ones - NOT to first, most to left placed, character.
> Why it works like that? What builtn-in function can format output, to make every character be placed as i need - relative to the first character, placed most to left side of screen.
>
str.rjust(x) will right justify "str" in a field of "x" width. This
is done by adding enough spaces to the left to make the length of the
result string (spaces + "str") equal to the specified width. The method
works on strings, not on output lines.
Make sure your display is using fixed width fonts, not variable.
>>> strs = ["1", "to", "two", "long term", "short"]
>>> for s in strs:
... print s.rjust(8), s.rjust(10), s.ljust(8), s.ljust(10),
s.center(10)
...
1 1 1 1 1
to to to to to
two two two two two
long term long term long term long term long term
short short short short short
(Note that xjust doesn't trim a long string, which is why the line of
"long term" does not align)
>>> for s in strs:
... print s[:8].rjust(8), s.rjust(10), s[:8].ljust(8), s.ljust(10),
s.center(10)
...
1 1 1 1 1
to to to to to
two two two two two
long ter long term long ter long term long term
short short short short short
In the above samples, I have specified an output line containing 5
"fields" of sizes 8, 10, 8, 10, 10, and using right, right, left, left,
centered justification.
If you need to layout a whole line, where some positions are being
set based on indexes, you need to do that as one array, not a collection
of substrings.
>>> match = [2, 7, 9, 13, 14, 24, 4]
dummy data, note that order doesn't matter
>>> buffer = [" "] * max(match)
build empty array sized to needed output
>>> print repr(buffer)
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '
', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
>>> for m in match:
... buffer[m - 1] = "|"
...
replace space with pipe for each position in match (match is
presumed to count from 1, but Python indexes from 0)
>>> print repr(buffer)
[' ', '|', ' ', '|', ' ', ' ', '|', ' ', '|', ' ', ' ', ' ', '|', '|', '
', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|']
>>> output = "".join(buffer)
join the position contents together to make the final output line
>>> print repr(output)
' | | | | || |'
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
How does .rjust() work and why it places characters relative to previous one, not to first character - placed most to left - or to left side of screen? crispy <ryniek90@gmail.com> - 2012-08-19 09:25 -0700
Re: How does .rjust() work and why it places characters relative to previous one, not to first character - placed most to left - or to left side of screen? crispy <ryniek90@gmail.com> - 2012-08-19 09:35 -0700
Re: How does .rjust() work and why it places characters relative to previous one, not to first character - placed most to left - or to left side of screen? Dave Angel <d@davea.name> - 2012-08-19 13:31 -0400
Re: How does .rjust() work and why it places characters relative to previous one, not to first character - placed most to left - or to left side of screen? crispy <ryniek90@gmail.com> - 2012-08-19 12:25 -0700
Re: How does .rjust() work and why it places characters relative to previous one, not to first character - placed most to left - or to left side of screen? crispy <ryniek90@gmail.com> - 2012-08-19 12:25 -0700
Re: How does .rjust() work and why it places characters relative to previous one, not to first character - placed most to left - or to left side of screen? Peter Otten <__peter__@web.de> - 2012-08-20 14:45 +0200
Re: How does .rjust() work and why it places characters relative to previous one, not to first character - placed most to left - or to left side of screen? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-19 14:56 -0400
csiph-web