这是我尝试并得到的错误:
$ENV:TEMP
错误:
a = 1
b = 2
c = 3
d = 4
e = 5
if(1):
get = str(a) +"," #Line 1
+str(b) +"," #Line 2
+str(c) +"," #Line 3
+str(d) +"," #Line 4
+str(e) #Line 5
else:
get = ",,,,,"
print(get)
然后我尝试删除空格:
File "testingpython.py", line 8
+str(b) +"," #Line 2
^
IndentationError: unexpected indent
错误:
a = 1
b = 2
c = 3
d = 4
e = 5
if(1):
get = str(a) +"," #Line 1
+str(b) +"," #Line 2
+str(c) +"," #Line 3
+str(d) +"," #Line 4
+str(e) #Line 5
else:
get = ",,,,,"
print(get)
请告诉我,当将字符串值放在单独的行上时如何将其分配给变量。
答案 0 :(得分:6)
在Python中,我们通常使用\
符号将单个语句分成多行 iff ,该语句不在括号内,如下所示:
result = "a" + "b" + "c"
可以表示为:
result = "a" + \
"b" + \
"c"
或者如果语句带有括号,则不需要\
为:
result = ("a" +
"b" +
"c")
答案 1 :(得分:4)
最简单的解决方案是将行包裹在括号内以指示它们属于同一行:
if(1):
get = ( str(a) +"," #Line 1
+str(b) +"," #Line 2
+str(c) +"," #Line 3
+str(d) +"," #Line 4
+str(e) #Line 5
)
else:
get = ",,,,,"
print(get)
但是您可以使用更短的方法:
get = ','.join(str(s) for s in [a, b, c, d, e])