Format character to center strings
As a string can be left-justified (within a given width) using e.g. '%-20s' % s and right-justified using '%+20s' % s # or '%20s' % s why not allow a string to be centered using '%=20s' %s Rob Cliffe
because you should use '{:^20}'.format(s) instead of the obsolete % stuff. Rob Cliffe <rob.cliffe@btinternet.com> schrieb am Sa., 28. März 2015 um 16:01 Uhr:
As a string can be left-justified (within a given width) using e.g. '%-20s' % s and right-justified using '%+20s' % s # or '%20s' % s why not allow a string to be centered using '%=20s' %s
Rob Cliffe _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
On Sat, Mar 28, 2015 at 1:07 PM, Philipp A. <flying-sheep@web.de> wrote:
because you should use '{:^20}'.format(s) instead of the obsolete % stuff.
"% stuff" is not "obsolete", but it follows C standard by design and any new features that do not come from C will likely be rejected.
Centered=same amount of white space on the left and to the right of the text. This depends on the font being used, especially whether it is a fixed or proportional font. So you would possibly have to also specify the font face and font size being used. Programming is not like a word processor where the font is specified within each document. Run this program to see the display difference between two "sentences" that are the same length, and between proportional and fixed fonts. #! /usr/bin/python try: import Tkinter as tk ## Python 2.x except ImportError: import tkinter as tk ## Python 3.x class DifferentFonts(): def __init__(self): self.top=tk.Tk() lit_1="Display lots of i's and l's-iiilll" lit_2="This is the same length as 1-wwwqq" ctr = 0 for each_font in (('Verdana', 12), ('Fixed', 12)): tk.Label(self.top, text=each_font[0]+"-"*30, font=each_font).grid(row=ctr) ctr += 1 tk.Label(self.top, text=lit_1, font=each_font).grid(row=ctr, sticky="w") ctr += 1 tk.Label(self.top, text=lit_2, font=each_font).grid(row=ctr, sticky="w") ctr += 1 tk.Button(self.top, text="Quit", bg="orange", command=self.top.quit).grid(row=20) self.top.mainloop() DifferentFonts() On 3/28/15, Rob Cliffe <rob.cliffe@btinternet.com> wrote:
As a string can be left-justified (within a given width) using e.g. '%-20s' % s and right-justified using '%+20s' % s # or '%20s' % s why not allow a string to be centered using '%=20s' %s
Rob Cliffe _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- Only when you actually get to the state where there is neither delusion nor enlightenment are you finally comfortable...Foyan
On Sat, Mar 28, 2015 at 10:11:49AM -0700, David Blaschke wrote:
Centered=same amount of white space on the left and to the right of the text. This depends on the font being used, especially whether it is a fixed or proportional font.
You're over-thinking it. Python already offers two ways of centering strings, with the distance measured in number-of-characters rather than pixels or points. py> "Hello world!".center(20) ' Hello world! ' py> "{:^20}".format("Goodbye everyone!") ' Goodbye everyone! ' Left and right justification also pad the string to a fixed width in characters, not in pixels. -- Steve
On 28/03/2015 14:56, Rob Cliffe wrote:
As a string can be left-justified (within a given width) using e.g. '%-20s' % s and right-justified using '%+20s' % s # or '%20s' % s why not allow a string to be centered using '%=20s' %s
Rob Cliffe
This http://bugs.python.org/issue23624 was rejected so I see two chances of this happening, zero or none. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
On 28/03/2015 18:11, Mark Lawrence wrote:
On 28/03/2015 14:56, Rob Cliffe wrote:
As a string can be left-justified (within a given width) using e.g. '%-20s' % s and right-justified using '%+20s' % s # or '%20s' % s why not allow a string to be centered using '%=20s' %s
Rob Cliffe
This http://bugs.python.org/issue23624 was rejected so I see two chances of this happening, zero or none.
Except that that referred to a proposed change that would break existing code. This is a proposed new feature which wouldn't. (And for what it's worth, I too would prefer it to add an odd pad character on the right.)
participants (6)
-
Alexander Belopolsky -
David Blaschke -
Mark Lawrence -
Philipp A. -
Rob Cliffe -
Steven D'Aprano