Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
output formating
#1
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
Reply
#2
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.
Reply
#3
>>> 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 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formating generated .data file to XML malcoverc 3 1,369 Apr-14-2022, 09:41 PM
Last Post: malcoverc
  propper formating paracelsusx 2 1,910 Jul-16-2021, 09:17 AM
Last Post: perfringo
  Adding graph points and formating project_science 4 2,407 Jan-24-2021, 05:02 PM
Last Post: project_science
  Excel: Apply formating of a sheet(file1) to another sheet(file2) lowermoon 1 2,059 May-26-2020, 07:57 AM
Last Post: buran
  tuple and formating problem darktitan 7 3,418 Feb-17-2019, 07:37 PM
Last Post: marienbad
  pymysql: formating ouput of query wardancer84 18 8,400 Oct-04-2018, 01:54 PM
Last Post: wardancer84

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020