Python | Get the numeric prefix of given string
Given a string. the task is to print numeric prefix in string (if it is present in string). Given below are few methods to solve the task.
Method #1: Using Naive Method
Python3
ini_string = " 123abcjw "
ini_string2 = "abceddfgh"
print ("initial string : ", ini_string, ini_string2)
res1 = ' '.join(c for c in ini_string if c in ' 0123456789 ')
res2 = ' '.join(c for c in ini_string2 if c in ' 0123456789 ')
print ("first string result: ", str (res1))
print ("second string result: ", str (res2))
|
Output:
initial string : 123abcjw abceddfgh
first string result: 123
second string result:
Time Complexity: O(n + m), where n + m are the length of given strings
Auxiliary Space: O(n + m)
Method #2: Using takewhile
Python3
from itertools import takewhile
ini_string = " 123abcjw "
ini_string2 = "abceddfgh"
print ("initial string : ", ini_string, ini_string2)
res1 = ''.join(takewhile( str .isdigit, ini_string))
res2 = ''.join(takewhile( str .isdigit, ini_string2))
print ("first string result: ", res1)
print ("second string result: ", res2)
|
Output:
initial string : 123abcjw abceddfgh
first string result: 123
second string result:
Time complexity: O(n), where n is the length of the string, since the code uses a for loop to iterate through the string and calls the built-in method str.isdigit() for each character in the string.
Auxiliary space: O(m), where m is the length of the numeric prefix in the string.
Method #3: Using re.sub
Python3
import re
ini_string = " 123abcjw "
ini_string2 = "abceddfgh"
print ("initial string : ", ini_string, ini_string2)
res1 = re.sub( '\D.*' , '', ini_string)
res2 = re.sub( '\D.*' , '', ini_string2)
print ("first string result: ", str (res1))
print ("second string result: ", str (res2))
|
Output:
initial string : 123abcjw abceddfgh
first string result: 123
second string result:
Time Complexity: O(n), where n is the length of the string.
Auxiliary Space: O(1).
Using Method #4: Using re.findall
Python3
import re
ini_string = " 123abcjw "
ini_string2 = "abceddfgh"
print ("initial string : ", ini_string, ini_string2)
res1 = ' '.join(re.findall(' \d + ', ini_string))
res2 = ' '.join(re.findall(' \d + ', ini_string2))
print ("first string result: ", str (res1))
print ("second string result: ", str (res2))
|
Output:
initial string : 123abcjw abceddfgh
first string result: 123
second string result:
Time complexity: The time complexity of this program is O(n), where n is the length of the input string. The reason is that we perform a single pass over the string to extract the numeric prefix using the re.findall() function.
Auxiliary space: The auxiliary space used by this program is O(1), as we only use a few variables to store the input string, the regex pattern, and the extracted numeric prefix. The space used by these variables does not depend on the length of the input string. Therefore, the space complexity is constant.
Method #5: Using isalpha() method
Python3
def fun(s):
x = ""
for i in s:
if i.isalpha():
break
else :
x + = i
return x
ini_string = "123abcjw"
ini_string2 = "abceddfgh"
print ( "initial string : " , ini_string, ini_string2)
res1 = fun(ini_string)
res2 = fun(ini_string2)
print ( "first string result: " , res1)
print ( "second string result: " , res2)
|
Output
initial string : 123abcjw abceddfgh
first string result: 123
second string result:
Time Complexity : O(N)
Auxiliary Space : O(N)
Method #6: Using re.search()
In the above code, we are using the search function from the re module to find the longest prefix of numeric characters in the input string. The regular expression r’^\d+’ matches one or more digits at the beginning of the string (the ^ symbol represents the start of the string).
If a match is found, the search function returns a Match object representing the match. We can use the group method of this object to extract the matched string. If no match is found, search returns None, and we return an empty string instead.
Finally, we use the print function to output the resulting prefix for each example input string.
Python
import re
def get_numeric_prefix(s):
match = re.search(r '^\d+' , s)
if match:
return match.group()
else :
return ""
print (get_numeric_prefix( "123abcjw" ))
print (get_numeric_prefix( "abceddfgh" ))
print (get_numeric_prefix( "123abcjw12" ))
|
Time Complexity O(n), where n is the length of the input string.
Auxiliary Space: O(n).
Last Updated :
08 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...