user_input = input("INPUT: ")
# 2x-1=1 6
left = user_input.split("=")[0]
right = user_input.split("=")[1]
print(left, right)
left_term = left.replace(" ",", ")
left_term = left.replace("-",",-")
right_term = right.replace(" ",", ")
right_term = right.replace("-",",-")
print(left_term,right_term)
terms_x = []
It just shows me 2x, -1 1 6
but i want is 2x, -1 1, 6
So, main question is how to replace " " with other string.
CodePudding user response:
You can replace directly the main string
before splitting from =
like this:
user_input = input("INPUT: ")
# 2x-1=1 6
spl = user_input.replace(" ",", ").replace("-",", -").split("=")
left = spl[0]
right = spl[1]
print(left, right)
terms_x = []
So the output would be:
2x, -1 1, 6
CodePudding user response:
The reason why your code was not working was because you're storing
right_term = right.replace(" ",", ")
and again overwriting the value of right_term by
right_term = right_term.replace("-",",-")
because of which the previously replaced string is not being used anymore, hence fixing your code would look like:
user_input = input("INPUT: ")
# 2x-1=1 6
left = user_input.split("=")[0]
right = user_input.split("=")[1]
print(left, right)
left_term = left.replace(" ",", ")
left_term = left_term.replace("-",",-")
right_term = right.replace(" ",", ")
right_term = right_term.replace("-",",-")
print(left_term,right_term)
terms_x = []
have added this so you could know what was actually causing it to not work.