How do you read from stdin?

后端 未结 22 2598
余生分开走
余生分开走 2020-11-21 06:46

I\'m trying to do some of the code golf challenges, but they all require the input to be taken from stdin. How do I get that in Python?

22条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-21 07:32

    import sys
    
    for line in sys.stdin:
        print(line)
    

    Note that this will include a newline character at the end. To remove the newline at the end, use line.rstrip() as @brittohalloran said.

提交回复
热议问题