0

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.


4
Contributors
6
Replies
8
Views
6 Years
Discussion Span
Last Post by FAITH2011
0

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 by winmic: clarification

1

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.
0

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 by richieking: n/a

0

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 by Gribouillis: n/a

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.