How to write multiple strings in one line?

后端 未结 11 642
谎友^
谎友^ 2020-12-01 03:28

I\'ve started to learn Python with LPTHW and I\'ve gotten to exercise 16:

http://learnpythonthehardway.org/book/ex16.html

And feel like an idiot because I ca

11条回答
  •  粉色の甜心
    2020-12-01 04:00

    Don't try to overcomplicate it. The string works the same as when you print something, except you are starting with target.write instead so you can write it to the file instead of printing.

    For example, if you were going to print the variables and put them on a new line each you would use this string

    print(f"{line1} \n{line2} \n{line3} \n")

    In exercise 5, you learnt that to pull in a variable you need to put {} around the name you gave to the variable and if you want to pull variables into a string, you must start your string with f" In exercise 9, you learnt to use \n if you want to put something on a new line

    Now you want to write to the file instead of printing it so the string is the same except it starts with target.write this time target.write(f"{line1} \n{line2} \n{line3} \n")

提交回复
热议问题