Introduction
In this project, you will learn how to extract integers from user input using a Python script and regular expressions. This project is designed to help you understand the basics of regular expressions and how to apply them in a practical programming task.
👀 Preview
~/project/$ python find_int.py
a1b2c3d4
1 2 3 4
~/project/$ python find_int.py
1 2 3 4
1 2 3 4
~/project/$ python find_int.py
12 3ad5
12 3 5
🎯 Tasks
In this project, you will learn:
- How to create a Python script to handle user input
- How to use regular expressions to find and extract integers from a string
- How to process and format the extracted integers for output
🏆 Achievements
After completing this project, you will be able to:
- Write a Python script that can extract numeric data from user input
- Understand the fundamentals of regular expressions and how to apply them in Python
- Demonstrate your ability to combine Python programming and regular expression techniques to solve a practical problem
Set Up the Python Script
In this step, you will create a new Python script file and import the necessary module.
- Open a text editor and create a new file named
find_int.pyin the/home/labex/projectdirectory. - At the beginning of the file, import the
re(regular expression) module. This module will be used to extract integers from the user's input.
import re
Define the Function to Extract Integers
In this step, you will create a function that takes a user's input and extracts all the integers from it.
- Define a function called
extract_integersthat takes a single argumentuser_input.
def extract_integers(user_input):
## Code will be added in the next step
pass
Implement the Integer Extraction Logic
In this step, you will implement the logic to extract integers from the user's input using regular expressions.
- Inside the
extract_integersfunction, use there.findall()method to find all the sequences of digits (integers) in theuser_inputstring.
def extract_integers(user_input):
integers = re.findall(r"\d+", user_input)
return " ".join(integers)
The r"\d+" regular expression pattern matches one or more digits (0-9). The re.findall() function returns a list of all the matched integers.
- The function then joins the extracted integers into a space-separated string and returns it.
Handle user input and print the result
In this final step, you will prompt the user for input, call the extract_integers function, and print the result.
- Add the following code at the end of the
find_int.pyfile:
if __name__ == "__main__":
user_input = input()
result = extract_integers(user_input)
print(result)
This code checks if the script is being run directly (not imported as a module) and then prompts the user for input, calls the extract_integers function with the user's input, and prints the resulting space-separated string of integers.
Now, your find_int.py script is complete. You can run the script and test it with different user inputs.
~/project$ python find_int.py
a1b2c3d4
1 2 3 4
~/project$ python find_int.py
1 2 3 4
1 2 3 4
~/project$ python find_int.py
12 3ad5
12 3 5
Congratulations! You have completed the project of extracting integers from user input using a Python script and regular expressions.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



