Explaine me what difference between this codeblocks and why 1st doesn`t do anything.(Need to create username using first and second name)

Question:

Why .lower and .replece didn`t do anything with names?
1st:

names = ["Joey Tribbiani", "Monica Geller", "Chandler Bing", "Phoebe Buffay"]
usernames = []

for name in names:
    name.lower()
    name.replace(' ', '_')
    usernames.append(name)
print(usernames)

2nd:

names = ["Joey Tribbiani", "Monica Geller", "Chandler Bing", "Phoebe Buffay"]
usernames = []

for name in names:
    usernames.append(name.lower().replace((' ', '_')))
print(usernames)
Asked By: Yaroslav Rudenko

||

Answers:

In 1st, you need to "keep the changes" assigning the result to some variable, in your case, the same one:

names = ["Joey Tribbiani", "Monica Geller", "Chandler Bing", "Phoebe Buffay"]
usernames = []

for name in names:
    name = name.lower()
    name = name.replace(' ', '_')
    usernames.append(name)
print(usernames)

In 2nd, you need to provide 2 arguments to replace (the old value and the new one), your tupple is giving just one:

names = ["Joey Tribbiani", "Monica Geller", "Chandler Bing", "Phoebe Buffay"]
usernames = []

for name in names:
    usernames.append(name.lower().replace(' ', '_'))
print(usernames)
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.