#NEWLINE# def with_line_numbers( string ) : #NEWLINE# #INDENT# 'Adds line numbers to the given string in a right-justified left hand column.  For example:\nA two line\nstring\n\nwould be transformed into:\n1 A two line\n2 string\n  ' #NEWLINE# lines = string . splitlines ( ) #NEWLINE# if ( len ( lines ) == 0 ) : #NEWLINE# #INDENT# lines . append ( string ) #NEWLINE# #UNINDENT# width = len ( str ( len ( lines ) ) ) #NEWLINE# return os . linesep . join ( [ ( ( str ( ( i + 1 ) ) . rjust ( width ) + ' ' ) + line ) for ( i , line ) in enumerate ( line ) ] )