Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Extract String till Numeric

Improve Article
Save Article
  • Last Updated : 15 Mar, 2023
Improve Article
Save Article

Given a string, extract all its content till first appearance of numeric character.

Input : test_str = “geeksforgeeks7 is best” 
Output : geeksforgeeks 
Explanation : All characters before 7 are extracted. 

Input : test_str = “2geeksforgeeks7 is best” 
Output : “” 
Explanation : No character extracted as 1st letter is numeric.

Method #1: Using isdigit() + index() + loop

The combination of above functions can be used to solve this problem. In this, we check for first occurrence of numeric using isdigit() and index() is used to get required index till which content needs to be extracted.

Python3




# Python3 code to demonstrate working of
# Extract String till Numeric
# Using isdigit() + index() + loop
 
# initializing string
test_str = "geeks4geeks is best"
 
# printing original string
print("The original string is : " + str(test_str))
 
# loop to iterating characters
temp = 0
for chr in test_str:
     
  # checking if character is numeric,
  # saving index
  if chr.isdigit():
     temp = test_str.index(chr)
 
# printing result
print("Extracted String : " + str(test_str[0 : temp]))

Output

The original string is : geeks4geeks is best
Extracted String : geeks

Time complexity: O(n), where n is the length of the input string. .
Auxiliary space: O(1), as only constant amount of extra space is used to store the temporary index variable.

Method #2: Using regex()

This is yet another way in which this task can be performed. Using appropriate regex(), one can get content before possible numerics.

Python3




# Python3 code to demonstrate working of
# Extract String till Numeric
# Using regex()
import re
 
# initializing string
test_str = "geeks4geeks is best"
 
# printing original string
print("The original string is : " + str(test_str))
 
# regex to get all elements before numerics
res = re.findall('([a-zA-Z ]*)\d*.*', test_str)
 
# printing result
print("Extracted String : " + str(res[0]))

Output

The original string is : geeks4geeks is best
Extracted String : geeks

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3: Without any builtin methods

Initialize a string with all digits from 0 to 9, an empty string.Iterate a for loop on given string and add  the characters to empty string, if you find first numeric character(using in operator) break from the for loop

Python3




# Python3 code to demonstrate working of
# Extract String till Numeric
 
# initializing string
test_str = "geeks4geeks is best"
 
# printing original string
print("The original string is : " + str(test_str))
 
# loop to iterating characters
res=""
num="0123456789"
for i in test_str:
    if i in num:
        break
    else:
        res+=i
 
# printing result
print("Extracted String : " + str(res))

Output

The original string is : geeks4geeks is best
Extracted String : geeks

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 4: Using next() function and enumerate()

Uses the next() function and enumerate() to find the index of the first numeric character in the string. The next() function returns the next item in an iterator, which in this case is the index of the first numeric character. If no numeric character is found, len(test_str) is returned instead. Finally, the extracted string is printed

Python3




# Python3 code to demonstrate working of
# Extract String till Numeric
# Using next() function and enumerate()
 
# initializing string
test_str = "geeks4geeks is best"
 
# printing original string
print("The original string is : " + str(test_str))
 
# Finding the index of the first numeric character
index = next((i for i, c in enumerate(test_str) if c.isdigit()), len(test_str))
 
# printing result
print("Extracted String : " + str(test_str[0 : index]))

Output

The original string is : geeks4geeks is best
Extracted String : geeks

Time Complexity: O(n), where n is the length of the input string. The time complexity of the next() function is O(1) and the time complexity of the generator expression inside it is also O(1) in the worst case, so the overall time complexity of the program is O(n).
Auxiliary Space: O(1). The program uses constant auxiliary space to store the input string, index, and extracted string. It does not use any additional space that depends on the size of the input.

Method 5: Using list comprehension and isdigit() method. we can also use list comprehension along with the isdigit() method to extract the string until the first numeric character is encountered.

Python3




test_str = "geeks4geeks is best"
index = next((i for i, c in enumerate(test_str) if c.isdigit()), len(test_str))
extracted_str = test_str[:index]
print("Extracted String : " + extracted_str)

Output

Extracted String : geeks

Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(1), which is constant space. 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!