String Manipulation
Does this code look familiar from the insult generator project?
name = input("What's your name? ")
if name == "David" or name == "david":
print("Hello Baldy!")
else:
print("What a beautiful head of hair!")
Right now, if the user writes "DAVID" or "david", the if statement works correctly. However, "DaVID" does not give the correct output.
To the computer, " david", "dAviD", and "david" are completely different.
To simplify what the user typed in, we can add these functions to the end of the name of the variable:
.lower() = all letters are lower case.upper() = all letters are upper case.title() = capital letter for the first letter of every word.capitalize() = capital letter for the first letter of only the first word
.lower() = all letters are lower case
.upper() = all letters are upper case
.title() = capital letter for the first letter of every word
.capitalize() = capital letter for the first letter of only the first word
Comments
Post a Comment