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.
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.