简体   繁体   中英

Python: Formatting a list (which contains a list) for printing

I'm working on a project that translates input to Pig Latin (yeah, I'm sure you've never seen this one before...) and having trouble formatting my output.

(for the following, sentence = a list holding user input (phrase), split by phrase.split() )

sentence.remove(split)
final = map(str,sentence)
print "Final is (before formatting:", final
final = [sentence[0].capitalize()] , sentence[1:]
#finalFormat = ' '.join(final)
print "Final is", str(final).strip('[]')
#print "FinalFormat is", finalFormat
print "In Pig Latin, you said \"", ' '.join(map(str, final)), "\". Oink oink!"

What I get is: "In Pig Latin, you said "['Firstword'] ['secondword', 'thirdword'] "

What I am looking for is: "In Pig Latin, you said "Firstword secondword thirdword."

Based on my debug print statements it looks like my problem is still on the line (5 from the bottom):

    final = [sentence[0].capitalize()] , sentence[1:]

Thanks in advance!

Change this line:

final = sentence[0].capitalize() , sentence[1:]

To this:

final = [sentence[0].capitalize()] + sentence[1:]

You were mapping a tuple of a string and a list, to strings, rather than a list.

Note: using 'single"' quotes here will avoid "this\\"" ugliness.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM