3

I want remove the "+" sign from the input 'x+y' where x and y is an string(single digit) and print the result.

For example, I am entering 5+7 and it should display 57

Here is the code:

opr = input("Enter string").strip("+")
print(opr)

This code is not removing the "+" sign.

AzizStark
  • 1,390
  • 1
  • 17
  • 27

2 Answers2

13

You can use replace

opr = input("Enter string").replace("+","")
print(opr)
  • 1
    @Aziz: `strip` does not remove characters in the middle of a string. – Maurice Meyer Oct 11 '17 at 10:19
  • `>>> help(str.strip)` Help on method_descriptor: strip(...) S.strip([chars]) -> string or unicode Return a copy of the string S with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead. If chars is unicode, S will be converted to unicode before stripping – chakradhar kasturi Oct 11 '17 at 10:19
1

If your read the FineManual(tm), you'll find out that str.strip() only removes from the start and end of the string.

The solution here is of course to use str.replace("+", "")

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118