Harry Ward Harry Ward - 5 months ago 22
Python Question

Can I separate a string into words by removing spaces? (In python)

(In Python) I want to read a .txt file, take out the string and separate it into variables by separating the string with " ". Maybe this code will help!

t = open ("Userlist.txt","r")
if t.mode == "r":
y = t.readline
t.close()
if " " in y


this is where I want the string to be put into 2 variables as there will only be a string like this: USERNAME PASSWORD. Can I separate Username and password (and therefore store them as variables)?

Answer Source

Try this, file.txt contains two words - USERNAME PASSWORD

   with open("file.txt", "r") as f:
        username, password = f.read().split(" ")
        print(username)
        print(password)