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: 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 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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Sun, 19 Aug 2012 09:25:03 -0700 (PDT), crispy declaimed the following in gmane.comp.python.general: > 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/