Python Forum
output formating - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: output formating (/thread-3742.html)



output formating - mcmxl22 - Jun-19-2017

I left the code as raw text because it would not format correctly in code brackets.
I am wondering if there is a way to get this to print out the letters in the diamond pattern I set up?

---------------------------------------------------
import time

def red1():
   for i in range(7):
       time.sleep(1)
       formation = '''{}
                       {}   {}
                          {}'''.format('R','G','G','R')
       print "%s\r" % formation,
---------------------------------------------------------
current output:
Output:
R                              G  G R                       R
Desired output:
Output:
   R G     G    R



RE: output formating - zivoni - Jun-19-2017

You need to put correct number of spaces into your formation string, inside triple quotes there is no indentation, so you need to use something like
     formation = '''  {}
{}   {}
 {}'''.format('R', 'G', 'G', 'R')
or
     formation = "  {}\n{}   {}\n  {}".format('R', 'G', 'G', 'R')
And you can add your carriage return \r to end of formation string to avoid second string formatting.


RE: output formating - buran - Jun-19-2017

>>> print ' {} \n{} {}\n {} '.format('R', 'G', 'G', 'R')
 R 
G G
 R 
>>> print '{: ^3}\n{} {}\n{: ^3}'.format('R', 'G', 'G', 'R')
 R 
G G
 R