Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

python - How to write multiple strings in one line?

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('
')
target.write(line2)
target.write('
') 
target.write(line3)
target.write('
') 

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

target.write(line1 
, line2 
, line3 
)

Or:

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

Or:

target.write(%r 
, %r 
, %r 
) % (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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
target.write(line1 
, line2 
, line3 
)

' ' only make sense inside a string literal. Without the quotes, you don't have string literals.

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

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 " " but not around variables. If you did that, you might have gotten something like:

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

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 + '
' + line2 + '
' + line3 + '
')

Moving on,

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

Again 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 
', '%r 
', '%r 
') % (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 
%r 
%r 
') % (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
%r
%r
' % (line1, line2, line3))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...