Extra line in output when printing inside a loop

后端 未结 5 553
鱼传尺愫
鱼传尺愫 2021-01-11 17:33

I can\'t figure out why the code #1 returns an extra empty line while code #2 doesn\'t. Could somebody explain this? The difference is an extra comma at the end of the code

5条回答
  •  一个人的身影
    2021-01-11 18:15

    The trailing , in the print statement will surpress a line feed. Your first print statement doesn't have one, your second one does.

    The input you read still contains the \n which causes the extra linefeed. One way to compensate for it is to prevent print from issuing a linefeed of its own by using the trailing comma. Alternatively, and arguably a better approach, you could strip the newline in the input when you read it (e.g., by using rstrip() )

提交回复
热议问题