Hi folk, I fairly new to Python 2.7 and Python 3 and like to find out how I would get a print to appear in the middle of the output screen without putting in a load of spaces into a print statement. Please keep it simple for me.

Thanks for you time.


you may want to try pretty print (pprint), and change the indent to whatever you need to print it out in the middle of the screen (I put a link on the bottom of this post). I have never used pprint in this way so that may or may not work. Otherwise, I think the only way to do this is to make your own format that will print something out in the middle of the screen using whitespace. (by the way, this may not work if the screen can be different sizes)

pretty print -> http://docs.python.org/library/pprint.html?highlight=print#pprint.PrettyPrinter

Edited 5 Years Ago by winmic: clarification

Hi folk, I fairly new to Python 2.7 and Python 3 and like to find out how I would get a print to appear in the middle of the output screen without putting in a load of spaces into a print statement. Please keep it simple for me.

Thanks for you time.


The textwrap module can be useful too. Here is an example

>>> from textwrap import fill
>>> s = fill("""Hi folk, I fairly new to Python 2.7 and Python 3 and like to find out how I would get a print to appear in the middle of the output screen without putting in a load of spaces into a print statement. Please keep it simple for me.""", initial_indent=" "*15, subsequent_indent= " "*15, width = 70)
>>> print(s)
               Hi folk, I fairly new to Python 2.7 and Python 3 and
               like to find out how I would get a print to appear in
               the middle of the output screen without putting in a
               load of spaces into a print statement. Please keep it
               simple for me.

simple format print

stuff="""
      Hi folk, I fairly new to Python 2.7 and Python 3 and
      like to find out how I would get a print to appear in
      the middle of the output screen without putting in a
      load of spaces into a print statement. Please keep it
      simple for me.
      """
print("%20s"% stuff) ## increase the figure  for more indentation

flavours ;)

Edited 5 Years Ago by richieking: n/a

simple format print

stuff="""
      Hi folk, I fairly new to Python 2.7 and Python 3 and
      like to find out how I would get a print to appear in
      the middle of the output screen without putting in a
      load of spaces into a print statement. Please keep it
      simple for me.
      """
print("%20s"% stuff) ## increase the figure  for more indentation

flavours ;)

@richieking : your method works for literal strings only, while textwrap.fill() works for any string (eg dynamically generated strings). In fact, you're formatting the string by hand :)

Edited 5 Years Ago by Gribouillis: n/a

Hi sorted my spacing problem using this code moves text across screen. Thanks for all the replies...

print ('Hello World')
print ("\nThis is my second line")
# \t = tab, which moves the text along the screen
print ("\t\tThis is my  Line")
This question has already been answered. Start a new discussion instead.