Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I get the group number from the user?

I want to get the group number by using input() function.

import re

text = "protocol internet python"
group_number = input("enter the group number : ")
pattern = re.finditer(r"(\w+)\s(\w+)\s(\w+)", text)
for searching in pattern:
    print(searching.group(group_number))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Possible solution is the following:

import re

text = "protocol internet python"
group_number = int(input("enter the group number : "))
result = [match.groups() for match in re.finditer(r"(\w+)\s(\w+)\s(\w+)", text)]

print(result[0][group_number])

Prints

enter the group number : 0
protocol

import re

text = "protocol internet python"
group_numbers = input("enter groups numbers separated with space: ") # 0 1
group_numbers = [int(_) for _ in group_numbers.split()]

result = [match.groups() for match in re.finditer(r"(\w+)\s(\w+)\s(\w+)", text)]

for group in group_numbers:
    print(result[0][group])

Prints

enter groups numbers separated with space: 0 1
protocol
internet
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading