I am writing a program where the user is asked to type in his name.
If the name starts with a, b or c, the program should print ("Your name starts with a, b or c").
Unfortunately if the user starts by typing in a space and then typing his name the program thinks the name starts with a space and it automatically prints "Your name doesn't start with a, b or c" even if the name starts with these letters.
I want to delete the space in the input now so this problem doesn't occure any longer.
So far I've tried if name.startswith((" ")): name.replace(" ", "")
Thanks for any help!
name = input("Hi, who are you?")
if name.startswith((" ")):
name.replace(" ", "")
if name.startswith(('a', 'b', 'c')):
print("Your name starts with a, b or c")
print(name)
else:
print("Your name doesn't start with a, b or c")
print(name)
strip?str.replacereturns a new string, which you're not using.str.lstripor similar.a,b, orcalways going to be single characters? You could sayif name[0] in 'abc':if re.match(r'^\s*[abc]', name)works as well, without the need to test for and optionally remove leading spaces.