write multiple lines in a file in python

前端 未结 8 1831
长情又很酷
长情又很酷 2020-12-03 08:04

I have the following code:

line1 = raw_input(\"line 1: \")
line2 = raw_input(\"line 2: \")
line3 = raw_input(\"line 3: \")
print \"I\'m going to write these          


        
相关标签:
8条回答
  • 2020-12-03 08:28

    It can be done like this as well:

    target.write(line1 + "\n" + line2 + "\n" + line3 + "\n")
    
    0 讨论(0)
  • 2020-12-03 08:36

    Assuming you don't want a space at each new line use:

    print("I'm going to write these to the file")
    target.write("%s\n%s\n%s\n" % (line1, line2, line3))
    

    This works for version 3.6

    0 讨论(0)
提交回复
热议问题