w3resource logo
Python Exercises

Python: Search the numbers (0-9) of length between 1 to 3 in a given string

Python Regular Expression: Exercise-18 with Solution

Write Python program to search the numbers (0-9) of length between 1 to 3 in a given string.

Sample Solution:-

Python Code:

import re
results = re.finditer(r"([0-9]{1,3})", "Exercises number 1, 12, 13, and 345 are important")
print("Number of length 1 to 3")
for n in results:
     print(n.group(0))
	 

Sample Output:

Number of length 1 to 3                                                                                       
1                                                                                                             
12                                                                                                            
13                                                                                                            
345 

Python Code Editor:

import re
results = re.finditer(r"([0-9]{1,3})", "Exercises number 1, 12, 13, and 345 are important")
print("Number of length 1 to 3")
for n in results:
     print(n.group(0))

Improve this sample solution and post your code through Disqus