0

I'm very new to Python and am working on some code to manipulate equations. So far, my code asks the user to input an equation. I want my code to be able to take something like "X + 1 = 2" and turn it into "x+1=2" so that the rest of my code will work, no matter the format of the entered equation.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 2
    Please update your question with the code you have tried. – quamrana Feb 14 '21 at 22:30
  • Does this answer your question? [Strip spaces/tabs/newlines - python](https://stackoverflow.com/questions/10711116/strip-spaces-tabs-newlines-python) – mkrieger1 Feb 14 '21 at 22:35
  • Does this answer your question? [How to strip all whitespace from string](https://stackoverflow.com/questions/3739909/how-to-strip-all-whitespace-from-string) – Tomerikoo Feb 14 '21 at 22:47

2 Answers2

1

A simple string .replace(old, new) followed by a .lower() will be sufficient.

"X + 1 = 2".replace(" ", "").lower() # 'x+1=2'
"X + 1   = 2".replace(" ", "").lower() # 'x+1=2'

for a more thorough replacement of all white space characters and not just spaces use python's re module:

import re 

re.sub(r'\s+', '', "X + 1   = 2").lower() # 'x+1=2'
Stefan B
  • 1,617
  • 4
  • 15
1

To convert to lower case and strip out any spaces use lower and replace.

the_input = 'X + 1 = 2'

the_output = the_input.replace(' ', '').lower()

# x+1=2
norie
  • 9,609
  • 2
  • 11
  • 18