如何在一行中写入多个字符串? [英] How to write multiple strings in one line?

查看:59
本文介绍了如何在一行中写入多个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始通过 LPTHW 学习 Python,并且已经开始练习 16:

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)

或者:

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

或者:

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

我只是无法在同一行中重写 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.

推荐答案

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

'\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')

好的,现在一切都是字符串文字.但是您希望 line1、line2、line3 不是字符串文字.你需要这些作为 python 表达式来引用有问题的变量.基本上,你必须在字符串周围加上引号,这些字符串实际上是像 "\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')

什么是2 2?没什么.您必须向 python 指定如何组合这两个部分.所以你可以有 2 + 22 * 22 2 没有任何意义.在这种情况下,我们使用 add 来组合两个字符串

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')

继续前进,

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

同样 \n 只在字符串文字中有意义.% 运算符用于生成字符串时将字符串作为其左侧.因此,您需要在字符串中包含所有格式细节.

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)

但是产生 3 个字符串文字,你只需要一个.如果你这样做了,写抱怨,因为它除了一个字符串,而不是 3.所以你可能已经尝试过:

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)

但是你想把line1、line2、line3写到文件里.在这种情况下,您正在尝试在写入完成后进行格式化.当python执行这个时,它会先运行target.write离开:

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)

这将没有任何用处.为了解决这个问题,我们需要将 % () 放在 .write()

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))

这篇关于如何在一行中写入多个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆