Find Numbers In String Python

Published On - 24 Apr 2024

In this article we will show you the solution of find numbers in string python, in Python, we can easily find numbers in strings using isdigit(), and isnumeric(). isdigit() and is numeric () both find the numbers in a string.

For example- “Ram buys pencil at Rs 5 and Virat buys pen at Rs 10”.

So this is the string we have to find the numbers present in this string while applying is digit and isnumeric() method it extracts all the numbers present in this string and give the result 5 and 10.

B ut the difference between isdigit() method and isnumeric method is that in isnumeric() method only works on the digit 0 to 9 but in isnumeric() method also works on fractional characters such as ⅓.

Step By Step Guide On Find Numbers In String Python :-

1. Using isdigit() method to find the numbers in string

a="John is 22 years old and rajesh is 21 years old"
b=a.split()
#print(b)
for i in b:
    if i.isdigit():
        print(i,end=" ") # 22 21
  1. We initialize a variable that represents the string.
  2. We can use the split() method to convert the string into a list of strings.
  3. We can use for loop to iterate each element present in the list.
  4. After that we can use the condition if the number is present in a string using the isdigit() method. So it prints the number.
a="John is 22 years old and rajesh is 21 years old"
b=a.split()
c=[int(i) for i in b if i.isdigit()]
print(c) #[22, 21]
  1. We initialize a variable that represents the string.
  2. We can use the split() method to convert the string into a list of strings.
  3. After that we can use list comprehension, to write the code in one line.
  4. Within list comprehension, we first use for loop to iterate each list element then use the isdigit() method to find numbers in string and convert the number into integer type.
def numbers(a):
    y=[]
    b=a.split()
    for i in b:
        if i.isdigit():
            y.append(int(i))
    return y
a=input()
print(numbers(a))
  1. We create a function named numbers.
  2. We create an empty list y that all the extracting numbers in a string append in the y.
  3. After that we convert the string into a list of strings using the split() method.
  4. We can use for loop to iterate each element present in the list.
  5. After that we can use the condition if the number is present in a string using the isdigit() method.
  6. Then the numbers which we extract in the list of string is appended into list y.
  7. We assign a variable a that takes input string from the user.

2. Using isnumeric() method to find the numbers in string

def numbers(a):
    y=[]
    b=a.split()
    for i in b:
        if i.isnumeric():
            y.append(i)
    return y
a=input()
print(numbers(a))
#input: There is ⅓ negative marking in test
#output: ['⅓']
  1. We create a function named numbers.
  2. We create an empty list y that all the extracting numbers in a string append in the y.
  3. After that we convert the string into a list of strings using the split() method.
  4. We can use for loop to iterate each element present in the list.
  5. After that we can use the condition if the number is present in a string using isnumeric() method but if we use the isdigit() method in this code example it returns the empty list because the isdigit() method does not work in fractional character.
  6. Then the numbers which we extract in the list of string is appended into list y.
  7. We assign a variable a that takes input string from the user.

Conclusion :-

In conclusion, we successfully learned, how to find numbers in the string.We use isdigit() and isnumeric() method.

Also, we learned the differences between these two methods. you can choose any one of the mentioned methods and code examples that suits you.

I hope this article on find numbers in string python helps you and the steps and method mentioned above are easy to follow and implement.