Reputation: 906
How can I convert the user input go 30,45
to go(30,45)
?
so far I have:
input = "go 30,45"
output = re.sub(r'go (\d+)', r'go(\1,\1)', input)
However this doesn't seem to be working, I know it is probably down to the (\1,\1) but what could I change to get the desired output?
Thanks
Upvotes: 2
Views: 99
Reputation: 142206
You can do this with an re
as demonstrated by @A.R.S, but in this case, I would probably just use str builtin methods...
'{0}({1})'.format(*"go 30,45".split())
Upvotes: 0
Reputation: 129537
Try this:
inpt = "go 30,45"
output = re.sub(r'go (\d+,\d+)', r'go(\1)', inpt) # 'go(30,45)'
Note that you should refrain from using input
as a variable name since it is already reserved.
Upvotes: 1