Skip to content
Related Articles

Related Articles

Improve Article

Python | Extract Numbers in Brackets in String

  • Last Updated : 22 Apr, 2020

Sometimes, while working with Python strings, we can have a problem in which we have to perform the task of extracting numbers in string that are enclosed in brackets. Lets discuss certain way in which this task can be performed.

Method : Using regex
The way to solve this task is to construct a regex string that can return in all the numbers in a string that have brackets around them.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




# Python3 code to demonstrate working of 
# Extract Numbers in Brackets in String
# Using regex
import re
  
# initializing string
test_str = "gfg is [1] [4] all geeks"
  
# printing original string
print("The original string is : " + test_str)
  
# Extract Numbers in Brackets in String
# Using regex
res = re.findall(r"\[\s*\+?(-?\d+)\s*\]", test_str)
  
# printing result 
print("Extracted number list : " + str(res)) 
Output :
The original string is : gfg is [1] [4] all geeks
Extracted number list : ['1', '4']
My Personal Notes arrow_drop_up
Recommended Articles
Page :