Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #27422

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?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'output': 0.04; 'processed': 0.05; 'that?': 0.05; 'character,': 0.07; 'function,': 0.07; 'lines.': 0.07; 'sized': 0.07; 'width': 0.07; 'subject:How': 0.09; 'python': 0.09; 'indexes': 0.09; 'output,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:characters': 0.09; 'width.': 0.09; 'subject:not': 0.11; 'aug': 0.13; '"long': 0.16; '"x"': 0.16; '"|"': 0.16; '(note': 0.16; '24,': 0.16; '[2,': 0.16; 'centered': 0.16; 'dummy': 0.16; 'left,': 0.16; 'match:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'strs': 0.16; 'subject:most': 0.16; 'subject:screen': 0.16; 'ter': 0.16; 'variable.': 0.16; 'string': 0.17; 'string,': 0.17; '>>>': 0.18; 'pipe': 0.22; 'specified': 0.23; 'first,': 0.27; 'replace': 0.27; "doesn't": 0.28; 'fixed': 0.28; 'header:X-Complaints-To:1': 0.28; 'character.': 0.29; 'spaces': 0.29; 'strings,': 0.29; 'array': 0.29; 'character': 0.29; 'relative': 0.30; 'function': 0.30; 'print': 0.32; 'subject: .': 0.33; 'url:home': 0.33; 'to:addr :python-list': 0.33; 'equal': 0.33; 'done': 0.34; 'needed': 0.35; 'data,': 0.35; 'subject:?': 0.35; 'received:org': 0.36; 'but': 0.36; 'characters': 0.36; 'method': 0.36; 'charset:us-ascii': 0.36; 'display': 0.36; 'enough': 0.36; 'does': 0.37; 'two': 0.37; 'being': 0.37; 'why': 0.37; 'ones': 0.37; 'previous': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'build': 0.39; 'space': 0.39; 'short': 0.39; 'where': 0.40; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'most': 0.61; 'matter': 0.61; 'subject:, ': 0.61; 'containing': 0.61; 'first': 0.61; 'side': 0.61; 'positions': 0.68; 'dennis': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
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 Sun, 19 Aug 2012 14:56:39 -0400
Organization > Bestiaria Support Staff <
References <db49919e-9c9f-4e1f-8dfe-2765c6717dbe@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host adsl-76-249-30-80.dsl.klmzmi.sbcglobal.net
X-Newsreader Forte Agent 3.3/32.846
X-No-Archive YES
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3523.1345402599.4697.python-list@python.org> (permalink)
Lines 76
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1345402599 news.xs4all.nl 6931 [2001:888:2000:d::a6]:33240
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:27422

Show key headers only | View raw


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 | NextPrevious in thread | Find similar | Unroll thread


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