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
  • 112,593
  • 23
  • 157
  • 280
  • 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
  • 1
    "Show me how to solve this coding problem" is [off-topic for Stack Overflow](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question). We expect you to make an [honest attempt at the solution](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), and then ask a specific question about your implementation. – martineau Feb 14 '21 at 22:36
  • 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,544
  • 3
  • 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,146
  • 2
  • 10
  • 17