Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6992909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:41:46+00:00 2026-05-27T19:41:46+00:00

I’ve started to learn Python with LPTHW and I’ve gotten to exercise 16: http://learnpythonthehardway.org/book/ex16.html

  • 0

I’ve started to learn Python with LPTHW and I’ve gotten to exercise 16:

http://learnpythonthehardway.org/book/ex16.html

And feel like an idiot because I can’t figure out one of the seemingly simple “extra credit” assignments that wants the following:

target.write(line1)
target.write('\n')
target.write(line2)
target.write('\n') 
target.write(line3)
target.write('\n') 

To be condensed to one line of code. I’ve tried some of the following:

target.write(line1 \n, line2 \n, line3 \n)

Or:

target.write('line1 \n, line2 \n, line3 \n')

Or:

target.write(%r \n, %r \n, %r \n) % (line1, line2, line3)

I just can’t get it to rewrite the line1, line2, and line3 strings all in the same line. And I’ve tried various other combinations with and without commas, quotes, etc. I keep getting varying errors, like Invalid Syntax or that I have too many arguments.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:41:47+00:00Added an answer on May 27, 2026 at 7:41 pm
    target.write(line1 \n, line2 \n, line3 \n)
    

    ‘\n’ only make sense inside a string literal. Without the quotes, you don’t have string literals.

    target.write('line1 \n, line2 \n, line3 \n')
    

    Ok, now everything is a string literal. But you want line1, line2, line3 to not be string literals. You need those as python expressions to refer the variables in question. Basically, you have to put quotes around strings that are actually text like “\n” but not around variables. If you did that, you might have gotten something like:

    target.write(line1 '\n' line2 '\n' line3 '\n')
    

    What is 2 2? It’s nothing. You have to specify to python how to combine the two pieces. So you can have 2 + 2 or 2 * 2 but 2 2 doesn’t make any sense. In this case, we use add to combine two strings

    target.write(line + '\n' + line2 + '\n' + line3 + '\n')
    

    Moving on,

    target.write(%r \n, %r \n, %r \n) % (line1, line2, line3)
    

    Again \n only makes sense inside a string literal. The % operator when used to produce strings takes a string as its left side. So you need all of that formatting detail inside a string.

    target.write('%r \n', '%r \n', '%r \n') % (line1, line2, line3)
    

    But that produce 3 string literals, you only want one. If you did this, write complained because it excepts one string, not 3. So you might have tried something like:

    target.write('%r \n%r \n%r \n') % (line1, line2, line3)
    

    But you want to write the line1, line2, line3 to the file. In this case, you are trying to the formatting after the write has already finished. When python executes this it will run the target.write first leaving:

    None % (line1, line2, line3)
    

    Which will do nothing useful. To fix that we need to to put the % () inside the .write()

    target.write('%r\n%r\n%r\n' % (line1, line2, line3))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.