Take the 2-minute tour ×

Here's the quiz question:

Adding two strings or making multiple copies of the same string.


greetings = "Hello World"

len(greetings) # get the length of string


greetings[0] # get the 1st character


print underline("Good Day")

 Good Day

# Write a function, given a string of characters, return the string together with '_'s of the same length.

My first attempt was:

def underline(title): 
  print title
  print len(title) * '_'

...which somewhat passes visually but returns a 'None' value also. (any idea why that is?) So instead I tried:

def underline(title): 
  print title, \nlen(title) * '_'

...and get an "unexpected character after line continuation character" error. Turning here after Google was less than helpful with this error.

share|improve this question
Mark as answer to the question which gave you solution –  Shashi Nov 2 '11 at 7:50
add comment

2 Answers

up vote 2 down vote accepted

Well you don't want to print those strings, you want to return them.

So create a string that combines them together (separated by a newline character) and return that.

def underline(title):
    return title + '\n' + len(title) * '_'
share|improve this answer
Thanks Jeff, knew I was making it harder than it needed to be. –  user746073 Nov 2 '11 at 7:46
add comment

Quote your newline

def underline(title): 
    print title, '\n', len(title) * '_'

Good point by Jeff (that's why your statement is returning None), I was pointing out what's causing this line continuation error.

As the error says, the line continuation character \ is not expecting any characters after it :)

share|improve this answer
Thanks Yuji, I guess every time I've used \n before, it's been within a string by necessity. Thanks for the helping hand. –  user746073 Nov 2 '11 at 7:48
add comment

Your Answer


