Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

.strip isn't removing the newline on certain strings

I’m trying to make a basic log-in system, and when I use .strip to remove newline characters (\n) off of two strings read from a file, it works on only one string, even though I use the same thing to remove the newlines from both. Here is my code.

def login():
    login_an = input("What is the name of the account you are trying to log in to?: ")
    an_file_name = login_an + ".txt"
    login_passw = input("What is the password to the account?")
    with open(an_file_name,"r") as file:
        account_name = file.readline()
        account_name.strip("\n")
        account_password = file.readline()
        account_password.strip("\n")
login()

The account_name string is the one that isn’t having the newline character at the end removed. What can I do to fix this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

.strip() does not modify the string in-place. It returns a new string with the whitespace removed. Your calls to .strip() should look like:

account_name = account_name.strip("\n")

rather than

account_name.strip("\n")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading