Given a string, extract all its content till first appearance of numeric character.
Input : test_str = “neveropen7 is best”
Output : neveropen
Explanation : All characters before 7 are extracted.Input : test_str = “2neveropen7 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 = "Lazyroar4Lazyroar 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])) |
The original string is : Lazyroar4Lazyroar is best Extracted String : Lazyroar
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 = "Lazyroar4Lazyroar 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 ])) |
The original string is : Lazyroar4Lazyroar is best Extracted String : Lazyroar
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 = "Lazyroar4Lazyroar 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)) |
The original string is : Lazyroar4Lazyroar is best Extracted String : Lazyroar
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 = "Lazyroar4Lazyroar 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])) |
The original string is : Lazyroar4Lazyroar is best Extracted String : Lazyroar
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 = "Lazyroar4Lazyroar 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) |
Extracted String : Lazyroar
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(1), which is constant space.
Method 6: Using re.match():
Steps:
- Initiate a for loop to traverse the string test_str
- If the character is a number then break out of the loop or else concatenate the characters to empty string res(checked using regular expression ^[0-9]+$ and re.match())
- Display res which contains string till numeric
Python3
# Python3 code to demonstrate working of # Extract String till Numeric # initializing string import re test_str = "Lazyroar4Lazyroar 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 re.match( '^[0-9]+$' , i): break else : res + = i # printing result print ( "Extracted String : " + str (res)) |
The original string is : Lazyroar4Lazyroar is best Extracted String : Lazyroar
Time Complexity: O(N) N – length of string test_str
Auxiliary Space: O(1) because we used single variable res to store result