Skip to content
Advertisement

is it possible to use “input variables” in custom order

I’m new in python and regex. I’m trying to make a one-line regex implementation. In first input want to take a text to check, and for second input the regex pattern. Is it possible in python to achieve that?

import re
print(len(re.findall('{regex}', '{text_to_check}')).format(text_to_check=input(), regex=input()))

edit: to make question clearer; due to expression console takes first input as regex into code. and second input as text for checking. I want first input to place for text for check and second one for regex.

PS: Sory for my bad english and thanks all for help.

Advertisement

Answer

So you want to take inputs on the same line as the function call, but reverse the order of them when calling the function? You can do that by collecting them into a list, reversing the list, and then calling the function with the splat operator.

print(len(re.findall(*[input("String:"), input("Regexp:")][::-1])))

There’s no need to use format(), since input() returns a string.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement