Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

How to extract numbers from a string in Python

To extract numbers from a string in Python, you can use a regular expression with the re module. The re module is part of the Python standard library and provides functions to work with regular expressions.

Here's an example of how to extract numbers (integers and decimals) from a string using the re module:

import re

# The input string
input_string = "There are 3 cats, 4.5 dogs, and 1 fish in the house."

# Define a regular expression pattern to match numbers
pattern = r"[-+]?\d*\.\d+|[-+]?\d+"

# Find all occurrences of the pattern in the input string
numbers = re.findall(pattern, input_string)

# Convert the matched strings to floats or integers
numbers = [float(num) if "." in num else int(num) for num in numbers]

# Print the extracted numbers
print(numbers)

Output:

[3, 4.5, 1]

In this example, we import the re module and define a regular expression pattern to match numbers (both integers and decimals). The pattern r"[-+]?\d*\.\d+|[-+]?\d+" can match positive and negative integers and decimals, including those with a leading decimal point (e.g., ".5"). We then use the re.findall() function to find all occurrences of the pattern in the input string.

After finding the numbers, we use a list comprehension to convert the matched strings to float or int data types. Finally, we print the extracted numbers.

If you only need to extract integers from a string, you can use a simpler pattern:

# Define a regular expression pattern to match integers
pattern = r"[-+]?\d+"

# Find all occurrences of the pattern in the input string
integers = re.findall(pattern, input_string)

# Convert the matched strings to integers
integers = [int(num) for num in integers]

# Print the extracted integers
print(integers)

Output:

[3, 1]

In this example, we use the pattern r"[-+]?\d+" to match positive and negative integers, and we extract only the integers from the input string.

  1. Python extract numbers from string:

    input_string = "abc 123 xyz 456"
    numbers = [int(s) for s in input_string.split() if s.isdigit()]
    
  2. Using regular expressions to extract numbers in Python:

    import re
    
    input_string = "abc 123 xyz 456"
    numbers = [int(num) for num in re.findall(r'\d+', input_string)]
    
  3. Extract floating-point numbers from string in Python:

    input_string = "abc 123.45 xyz 678.90"
    numbers = [float(num) for num in re.findall(r'\d+\.\d+', input_string)]
    
  4. Get digits from a string in Python:

    input_string = "abc 123 xyz 456"
    digits = [int(digit) for digit in input_string if digit.isdigit()]
    
  5. Extracting positive and negative numbers from string in Python:

    input_string = "abc -123 xyz 456"
    numbers = [int(num) for num in re.findall(r'-?\d+', input_string)]
    
  6. Extract decimal numbers from alphanumeric string in Python:

    input_string = "abc 123.45 xyz 678.90"
    numbers = [float(num) for num in re.findall(r'\b\d+\.\d+\b', input_string)]
    
  7. Using isdigit() for extracting numbers in Python:

    input_string = "abc 123 xyz 456"
    numbers = [int(s) for s in input_string.split() if s.isdigit()]
    
  8. Extract integer and decimal parts from string in Python:

    input_string = "abc 123.45 xyz 678.90"
    numbers = [float(part) for part in re.findall(r'\d+\.\d+|\d+', input_string)]
    
  9. Extract numbers with commas from string in Python:

    input_string = "abc 1,234 xyz 56,789"
    numbers = [int(num.replace(',', '')) for num in re.findall(r'\b\d{1,3}(?:,\d{3})*(?:\.\d+)?\b', input_string)]
    
  10. Extracting scientific notation numbers from string in Python:

    input_string = "abc 1.23e-4 xyz 5.6e7"
    numbers = [float(num) for num in re.findall(r'-?\d+(?:\.\d+)?(?:[eE][+\-]?\d+)?', input_string)]
    
  11. Splitting and extracting numbers using map() in Python:

    input_string = "abc 123 xyz 456"
    numbers = list(map(int, filter(str.isdigit, input_string.split())))
    
  12. Extracting currency values from string in Python:

    input_string = "abc $1,234.56 xyz £789.00"
    currencies = [float(num.replace(',', '').replace('£', '').replace('$', '')) for num in re.findall(r'\b\d+(?:,\d{3})*(?:\.\d+)?\b', input_string)]