0

i spent last night trying to deal with strings in Python3. It would be very helpful from your part if you could help me to resolve this problem.

So, suppose that i've a string like this :

a = "√22+34-4+√(2+3)/5+√3"

NB: the string a is a user input and may change every time.

i want to manage the string to be like this :

a = "√(22)+34-4+√(2+3)/5+√(3)"

then i can replace "√" by "sqrt" using a.replace("√","sqrt").

Any suggestions ? And sorry for my bad english :) Thanks

Chiheb Nexus
  • 9,104
  • 4
  • 30
  • 43
  • What you're trying to match is something like "√ followed by a string of digits, but not √ followed by an expression in parentheses", right? That's pretty hard to write in terms of pure `str` methods, but relatively easy to write in terms of regular expressions, or `pyparsing`, or almost anything else. – abarnert Oct 28 '14 at 01:02
  • If your question is really about how to modify a python string: you can't (at least not "in place"). You can create new strings based on the previous one, though. http://stackoverflow.com/questions/9189172/python-string-replace – Ricardo Cárdenes Oct 28 '14 at 01:05
  • @abarnet & Ricardo Cárdenes : What i want to do is to catch digits that follow √ and stopps when there is "+" or "-" or "*" or "/" or "(" or ")" . The answer of arshaji is good enough for my problem. I may use regular expression "re". Thanks a lot for your answers :) – Chiheb Nexus Oct 28 '14 at 01:58

1 Answers1

4

You could try re.sub():

>>> import re
>>> a = "√22+34-4+√(2+3)/5+√3"
>>> re.sub(r'√(\d+)', r'√(\1)', a)  # \1 is whatever was captured by (\d+)
'√(22)+34-4+√(2+3)/5+√(3)'

But if you need anything more sophisticated, you'll probably have to write a parser of some sort.

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • 1
    @nexus66 Note that `re.sub(...,...,a)` DOES NOT modify your `a` string, it creates a new string instead! If you want to use the new string (the one with parentheses) _you have to assign the new string to a variable_. A common pattern is simply `a=re.sub(...,...,a)` in which you throw away the old string to replace it with the new one. – gboffi Oct 28 '14 at 01:15
  • @arshajii : Yes ! That's what i'm looking for. I remember that i found the fonction re in my Python3 courses. i'll try to understand your answer. Thanks a lot for your quick answer :) – Chiheb Nexus Oct 28 '14 at 01:54
  • @gboffi : Yes ! The return of this function is a string. I put it inside my PyGObject code and it work well ! I'm trying to create a calculator using Python3 and Gtk3+. – Chiheb Nexus Oct 28 '14 at 02:05
  • @arshajii : Yes it work. And if i don't bother you, could you explain me how can i add the caption of the dot "." (digits and the dot) In documentation i found : re.sub(r'√(\d.+)', r'√(\1)', a) adding the dot before "+" but it doesn't work in every case. There're some errors. – Chiheb Nexus Oct 28 '14 at 02:49
  • 1
    @nexus66 What exactly are you trying to match with `\d.+`? – arshajii Oct 28 '14 at 02:51
  • example : a = "√0.5 + √22 + √(11) +7 " how can i put it like this ? : a = "√(0.5) + √(22) + √(11) +7 " – Chiheb Nexus Oct 28 '14 at 02:59
  • 1
    @nexus66 Try using `√(\d*\.?\d+)` as the regular expression. – arshajii Oct 28 '14 at 03:09
  • @arshajii : Yes ! It work perfectly ! Thanks a lot. With your last answer i can undertstand the documentation and deal with it. the link of documentation : https://docs.python.org/3.4/library/re.html – Chiheb Nexus Oct 28 '14 at 03:14