Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Extract String Till First Non-Alphanumeric Character
Python strings are sequences of characters that represent information or data. A normal string can contain various characters that are enclosed within single or double quotes, but an alphanumeric string only consists of digits and letters. Both alphanumeric and non-alphanumeric strings are used in various scenarios including password protection, data processing, and validation.
In this tutorial, we will extract a substring from the beginning of a string until we encounter the first non-alphanumeric character.
Understanding the Problem
We need to extract a substring from an original string before we encounter a non-alphanumeric character. Let's understand this with an example ?
Input Output Scenarios
Consider the following string ?
input_string = "Sales18@22!Roam"
print("Input:", input_string)
Input: Sales18@22!Roam
The given string consists of letters, digits, and special characters. We need to retrieve a substring as soon as we encounter a non-alphanumeric character ?
Output: Sales18
The substring "Sales18" is returned from the original string because after this, a non-alphanumeric character "@" was encountered.
Method 1: Using Iteration
This is a basic approach where we iterate through each character and check if it's alphanumeric. We break the loop when we find the first non-alphanumeric character ?
input_string = "Sales18@22Roam"
print(f"The original string is: {input_string}")
extracted_string = ""
alphanumeric_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
for char in input_string:
if char not in alphanumeric_chars:
break
else:
extracted_string += char
print(f"The extracted string till 1st Non-Alphanumeric character: {extracted_string}")
The original string is: Sales18@22Roam The extracted string till 1st Non-Alphanumeric character: Sales18
Method 2: Using Regex with search()
The re module provides powerful pattern matching capabilities. We use the search() function to find the first non-alphanumeric character using the pattern \W+ ?
import re
input_string = "Sales18@22Roam"
print(f"The original string is: {input_string}")
# Find the first non-alphanumeric character
match = re.search(r"\W+", input_string)
if match:
index = match.start()
print(f"The 1st non-alphanumeric character is encountered at: {index}")
extracted_string = input_string[:index]
else:
extracted_string = input_string
print(f"The extracted string till 1st Non-Alphanumeric character: {extracted_string}")
The original string is: Sales18@22Roam The 1st non-alphanumeric character is encountered at: 7 The extracted string till 1st Non-Alphanumeric character: Sales18
Method 3: Using Regex with findall()
This approach uses findall() to find all occurrences of alphanumeric substrings and returns the first one ?
import re
input_string = "Sales18@22Roam"
print(f"The original string is: {input_string}")
# Find all alphanumeric sequences
alphanumeric_sequences = re.findall(r"[\dA-Za-z]*", input_string)
extracted_string = alphanumeric_sequences[0]
print(f"The extracted string till 1st Non-Alphanumeric character: {extracted_string}")
The original string is: Sales18@22Roam The extracted string till 1st Non-Alphanumeric character: Sales18
Method 4: Using isalnum() Method
The isalnum() method checks if a character is alphanumeric. We iterate through the string and stop when we find a non-alphanumeric character ?
input_string = "Sales18@22Roam"
print(f"The original string is: {input_string}")
extracted_string = input_string # Default to full string
for i in range(len(input_string)):
if not input_string[i].isalnum():
extracted_string = input_string[:i]
print(f"The 1st non-alphanumeric character is encountered at: {i}")
break
print(f"The extracted string till 1st Non-Alphanumeric character: {extracted_string}")
The original string is: Sales18@22Roam The 1st non-alphanumeric character is encountered at: 7 The extracted string till 1st Non-Alphanumeric character: Sales18
Comparison
| Method | Complexity | Best For |
|---|---|---|
| Iteration | O(n) | Simple cases, learning |
| Regex search() | O(n) | Complex patterns |
| Regex findall() | O(n) | Multiple extractions |
| isalnum() | O(n) | Built-in method, readable |
Conclusion
We explored four different approaches to extract a substring until the first non-alphanumeric character. The isalnum() method provides the most readable solution, while regex methods offer more flexibility for complex patterns.
