2

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

4

2 に答える 2

1

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.

于 2012-11-18T16:21:39.480 に答える
0

@ARSで示されているように、これを行うことができますreが、この場合、おそらくstr組み込みメソッドを使用するだけです...

'{0}({1})'.format(*"go 30,45".split())
于 2012-11-18T16:28:51.583 に答える