import re
x = ‘My 2 favourite numbers are 29 and 42’
y = re.findall(’[0-9]+’ ,x)
print(y)
Kindly explain every single line of this program
import re
x = ‘My 2 favourite numbers are 29 and 42’
y = re.findall(’[0-9]+’ ,x)
print(y)
Kindly explain every single line of this program
Hey @shlok.sharma3, in this program we use the re module which stands for regex module. So this is usually used to find similar patterns as specified by the re.findall()
method. So in this case, we give a pattern as '[0-9]+'
. This regular pattern means that all the numbers from 0 to 9 (and + indicates one or more occurrences of numbers between 0 and 9) will be selected from the input string x.
Hence when we run the above program we get the following output :
['2', '29', '42']
For more information on using regex, please refer to this link.
I hope this clears your doubt !
Please mark the doubt as resolved in your doubts section !
Happy Learning !
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.