Mostly Linux & Python syntax notes and hyperlinks.

Tuesday, August 12, 2014

python : write a message with a dashed line of the same length above &/or below

testdash.py
def writeWithDashedLine(space_before,msg,before=False,after=True,dash='-'):
    string = ""
    dashes = dash * len(msg)
    if before:
        string += "%s%s\n" % (space_before,dashes)
    string += "%s%s\n" % (space_before,msg)
    if after:
        string += "%s%s\n" % (space_before,dashes)
    return string
mystring = writeWithDashedLine('     ','Hi this is a test')
print mystring

mystring = writeWithDashedLine('   ','Here is another',True,True,'=')
print mystring

output:
$ python testdash.py
     Hi this is a test
     -----------------

   ===============
   Here is another
   ===============

No comments:

Post a Comment