I have two transform cases:
s = "foo bar" #-> "foo bar &"
s = "foo ! bar" # -> "foo & ! bar" -> notice not '&!'
I did it like this:
t = s.split("!", 1)
t[0] = t[0] + " &"
" !".join(t)
What’s a more pythonic way to do the same?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
str.partitionis built for the purpose of operator parsing:It is adapted to prefix and infix operator when parsing from left to right. The fact it always returns a 3-tuple allows to use it even when your operator is not found and apply an action on your result as shown above.