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

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 ;)

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 :)

like i stated on the top.

simple format print

;)

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")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.