在python中为变量添加换行符

时间:2015-07-19 12:30:18

标签: python

我有以下功能来获得以下输出

22
4444
666666

相反,我正在

'22\n4444\n666666\n88888888\n'

任何我出错的想法?​​

def EvenLadder(n):
    ...:     solution = ''
    ...:     if n <= 1:
    ...:         return solution
    ...:     elif n%2 ==0:
    ...:         for i in range(2,n+1,2):
    ...:             solution += (str(i)*i)+"\n"
    ...:     else:
    ...:         n = n - 1
    ...:         for i in range(2,n+1,2):    
    ...:             solution += (str(i)*i)+"\n"
    ...:     return solution

1 个答案:

答案 0 :(得分:4)

'22\n4444\n666666\n88888888\n'是预期结果的正确字符串表示形式。为了实际处理换行符,您需要打印它:

print EvenLadder(6)