Open In App

Python | Get numeric prefix of given string

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with strings, we might be in a situation in which we require to get the numeric prefix of a string. This kind of application can come in various domains such as web application development. Let’s discuss certain ways in which this task can be performed. 

Method #1: Using re.findall() The regex can be used to perform this particular task. In this, we use findall() function which we use to get all the occurrences of numbers and then return the initial occurrence. 

Python3




# Python3 code to demonstrate working of
# Get numeric prefix of string
# Using re.findall()
import re
 
# initializing string
test_str = "1234Geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# Using re.findall()
# Get numeric prefix of string
res = re.findall('\d+', test_str)
 
# printing result
print("The prefix number at string : " + str(res[0]))


Output : 

The original string is : 1234Geeks
The prefix number at string : 1234

Time Complexity:  O(n), where n is the length of the string “test_str”. The function “re.findall()” iterates over the string “test_str” once to find all occurrences of the pattern ‘\d+’ (a sequence of one or more digits).
Auxiliary Space:  O(n), where n is the length of the prefix number found in “test_str”. The function “re.findall()” stores the result in a list, which takes up space proportional to the length of the prefix number.

Method #2: Using itertools.takewhile() The inbuilt function of takewhile can be used to perform this particular task of extracting all the numbers till a character occurs. 

Python3




# Python3 code to demonstrate working of
# Get numeric prefix of string
# Using itertools.takewhile()
from itertools import takewhile
 
# initializing string
test_str = "1234Geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# Using itertools.takewhile()
# Get numeric prefix of string
res = ''.join(takewhile(str.isdigit, test_str))
 
# printing result
print("The prefix number at string : " + str(res))


Output : 

The original string is : 1234Geeks
The prefix number at string : 1234

Method #3: Using isdigit() method 

Python3




# Python3 code to demonstrate working of
# Get numeric prefix of string
# initializing string
 
test_str = "1234Geeks"
 
# printing original string
print("The original string is : " + test_str)
 
s=""
for i in test_str:
    if(i.isdigit()):
        s+=i
    else:
        break
 
# printing result
print("The prefix number at string : " + str(s))


Output

The original string is : 1234Geeks
The prefix number at string : 1234

Time Complexity: O(n), where n is the length of the string. This is because the code performs a loop that iterates through each character of the string, and the processing time inside the loop is constant.
Auxiliary Space: O(n), because it creates a new string s to store the numeric prefix of the original string, and its length is at most the length of the original string.

Method 4 : using a regular expression pattern. 

step-by-step approach:

  1. Import the re module for regular expression matching.
  2. Initialize the input string to be processed.
  3. Define a regular expression pattern that matches all the consecutive digits at the beginning of the string. The pattern should start with ^ to match at the beginning of the string, followed by \d+ to match one or more digits, and end with $ to match the end of the string.
  4. Use the re.match() method to search for the regular expression pattern in the input string.
  5. Extract the matched digits using the group() method of the match object.
  6. Print the result.

Python3




import re
 
# initializing string
test_str = "1234Geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# regular expression pattern
pattern = r'^\d+'
 
# search for pattern in string
match = re.match(pattern, test_str)
 
if match:
    # extract matched digits
    s = match.group()
else:
    # no match found
    s = ""
 
# printing result
print("The prefix number in the string : " + str(s))


Output

The original string is : 1234Geeks
The prefix number in the string : 1234

Time Complexity: The time complexity of this approach is O(n), where n is the length of the input string.

Auxiliary Space: The space complexity of this approach is O(1), as only a constant amount of extra space is used to store the regular expression pattern and the matched digits.



Last Updated : 08 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads