1

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)
6
  • Have you tried removing the space using strip? Commented Jul 11, 2018 at 8:30
  • 2
    Strings are immutable. str.replace returns a new string, which you're not using. Commented Jul 11, 2018 at 8:31
  • 1
    You might want to use str.lstrip or similar. Commented Jul 11, 2018 at 8:33
  • Is the test for a, b, or c always going to be single characters? You could say if name[0] in 'abc': Commented Jul 11, 2018 at 8:41
  • A surprising number of other options. if re.match(r'^\s*[abc]', name) works as well, without the need to test for and optionally remove leading spaces. Commented Jul 11, 2018 at 8:48

4 Answers 4

2

As people have stated in the comments, strings are immutable. This means that you can't actually change the value of an existing string - but you can create a new string that includes the changes you want to make.

In your case, you are using the .replace() function - this function returns a new string after the replacement has happened. A simple example:

str = 'I am a string'
new_string = str.replace('string', 'boat')

Note that the variable new_string now contains the desired changes - "I am a boat" but the original str variable remains unchanged.

To answer your question directly, you'll need to use the variable you created after trimming the whitespace. You can even re-use the same variable:

if name.startswith((" ")):
    name = name.replace(" ", "") # override "name" with the new value

if name.startswith(('a', 'b', 'c')):
    ...
Sign up to request clarification or add additional context in comments.

Comments

0

String are imutable. No operation on a string will ever change this string

name.replace(" ", "") do not modify namebut return a new string and let name unchange

So you can write

new_name = name.replace(" ", "")

but you can also write

name = name.replace(" ", "")

In this case, the original string is not modified. But it's name is reused to receive the result of name.replace(" ", "")

Wich writing is the best depend on who you ask. I prefer the second one.

Comments

0

Strings are immutable and replace does not modify the string calling it, but returns a new string which you cold use directly on your code:

name = input("Hi, who are you?")

if name.replace(" ", "").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")

You could also create a new variable or just reassign name:

name = name.replace(" ", "")

in case you are going to use it further in your code.

Comments

0
# get name as user input and clean    
name = input("Hi, what is your name? ").lower().strip()

# conditional based on intial letter of name
print("Your name starts with a,b or c" if name.startswith(("a", "b", "c")) else "Your name doesn't start with a,b or c")

print(name.capitalize())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.