how can i print text files without \n in python

Viewed 130
open("usernames.txt", "r")
open("passwords.txt", "r")

with open("usernames.txt", "r") as users:
    usernames = users.readlines()

with open("passwords.txt", "r") as user_passwords:
    passwords = user_passwords.readlines()

print(usernames)
print(passwords)

i currently have this for my code, but it outputs it like this tenn\n i need to have it like this tenn

2 Answers

You could read like this:

with open("passwords.txt", "r") as user_passwords:
    passwords = user_passwords.read().splitlines()

This reads the entire file first and then splits them by lines.

Can also replace the \n with nothing

with open("passwords.txt", "r") as user_passwords:
    passwords = user_passwords.read().replace('\n', '')
Related