From http://pymbook.readthedocs.org/en/latest/modules.html
I've added some code to this code to make a little header for my code snippets as an exersise.

"""
Bars Module
============
This is an example module with provide different ways to print bars.
"""
def starbar(num):
    ''' (int) -> integer

    Prints a bar with *

    >>> bars.starbar(10)
    **********
    '''
    print('*' * int(num))

def hashbar(num):
    ''' (int) -> integer

    Prints a bar with #

    >>> bars.hashbar(10)
    ##########
    '''
    print('#' * int(num))

def simplebar(num):
    ''' (int) -> integer

    Prints a bar with -

    >>> bars.simplebar(10)
    ----------
    '''
    print('-' * int(num))

def lp_header():
    """
    Prints a header for my py files
    """
    hashbar(80)
    print "# Filename:\t\n# Copyright:\tLuke Pettit\n# Date:\t\t    /  /2013"
    hashbar(80)

and it prints this :-

################################################################################                                                                                          
# Filename:                                                                                                                                                               
# Copyright:    Luke Pettit                                                                                                                                               
# Date:             /  /2013                                                                                                                                              
################################################################################

What I would like to achieve is this:-

#-------------------------------------------------------------------------------                                                                                          
# Filename:                                                                                                                                                               
# Copyright:    Luke Pettit                                                                                                                                               
# Date:             /  /2013                                                                                                                                              
#-------------------------------------------------------------------------------

So putting a hash symbol and then a 79 line of - dashes
Can I somehow call two functions one after another and how would I format that
or do I rewite the code entirely?

The answer was as simple as

print('#' + '-' * (num-1))

I added this function to handle this situation

def hash_dashbar(num):
    ''' (int) -> integer

    Prints a bar with #

    >>> bars.hash_dashbar(10)
    #---------
    '''
    print('#' + '-' * (num-1))

It's a very good idea to use parenthesis in your print statements as if you were using a function. It teaches you python 3 at the same time. If you add the line

from __future__ import print_function

as the first statement of your module, it will turn print into a true function. Then you can use

print('#', '-' * num, sep='')

or

print('#', end = '') # don't print a newline
print('-' * num)

I am not such a friend of banners but here is one version for you to learn some other features of Python (as you did mention format in your title)

>>> def banner(filename, name, d, m, y=2013, width=80):
    print '#' * width
    print '''\
# Filename:     {filename}
# Copyright:    {name}
# Date:         {d}/{m}/{y}'''.format(filename=filename, name=name, d=d, m=m,y=y)
    print '#' * width


>>> banner('demo.py', 'Tony Veijalainen', 9, 9)
################################################################################
# Filename:     demo.py
# Copyright:    Tony Veijalainen
# Date:         9/9/2013
################################################################################
>>> 

Thanks pyTony and Gribouillis. I am learning P3. pyTony, this was exactly what I was heading for eventually.
Very much apperciated thanks to both of you.
Luke

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.