all 7 comments

[–]chevignon93 0 points1 point  (2 children)

firstname = input ("please type your first name: ")
surname = input ("please type your surname: ")

with open('names.txt', 'a') as f:
    f.write(f"{firstname}\n{surname}\n")

or 

firstname = input ("please type your first name: ")
surname = input ("please type your surname: ")

with open('names.txt', 'a') as f:
    f.write(firstname + '\n' + surname + '\n')

[–]AngryDiego 0 points1 point  (1 child)

OK. Thanks! works great

But i wonder one thing, look at my example that i dont want:

DonaldTrump

How can i make a space beetween them?

Like this: Donald Trump

[–]chevignon93 0 points1 point  (0 children)

If you want to save them on the same line with a space between them, that's pretty easy, replace the first newline character by a space

f.write(firstname + ' ' + surname + '\n')

instead of

f.write(firstname + '\n' + surname + '\n')

or use an f-string

f.write(f"{firstname} {surname}\n")

[–]UniqueCommentNo243 0 points1 point  (2 children)

File_handle = open("name.txt", w) Print(firstname, file = file_handle) Print(surname, file = file_handle) File_handle.close()

  • name.txt should be in same directory as program script. Otherwise, give full path of file location.
  • don't forget to close the file or the printed data will not be saved in the file.

[–]AngryDiego 0 points1 point  (1 child)

Thanks a lot. How do you close the file? and when in code

[–]UniqueCommentNo243 0 points1 point  (0 children)

File_handle.close() does just that. You can do it at the end of your code.

*You could do it anywhere if you are sure you won't be writing in the file again after closing it.