Python | Get the numeric prefix of given string
Last Updated :
08 Apr, 2023
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).
Similar Reads
Python | Get numeric prefix of given string
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.f
4 min read
Python - Get Nth word in given String
Sometimes, while working with data, we can have a problem in which we need to get the Nth word of a String. This kind of problem has many application in school and day-day programming. Let's discuss certain ways in which this problem can be solved. Method #1 : Using loop This is one way in which thi
4 min read
Sort given list of strings by part the numeric part of string - Python
We are given a list of strings containing both letters and numbers, and the goal is to sort them based on the numeric part within each string. To do this, we extract the digits, convert them to integers and use them as sorting keys. For example, in ["Gfg34", "is67", "be3st"], the numbers 34, 67, and
3 min read
Print Substrings that are Prefix of the Given String - Python
The task of printing the substrings that are prefixes of a given string involves generating all the possible substrings that start from the first character of the string and end at any subsequent position.For example, if the given string is s = "hello", the prefixes of the string would include "h",
3 min read
Python | Get the string after occurrence of given substring
The problem involves getting the string that is occurring after the substring has been found. Let's discuss certain ways in which this task can be performed using Python. Using partition()To extract the portion of a string that occurs after a specific substring partition() method is an efficient and
3 min read
Python program to count the number of characters in a String
The goal here is to count the number of characters in a string, which involves determining the total length of the string. For example, given a string like "GeeksForGeeks", we want to calculate how many characters it contains. Letâs explore different approaches to accomplish this. Using len()len() i
3 min read
Frequency of Numbers in String - Python
We are given a string and we have to determine how many numeric characters (digits) are present in the given string. For example: "Hello123World456" has 6 numeric characters (1, 2, 3, 4, 5, 6). Using re.findall() re.findall() function from the re module is a powerful tool that can be used to match s
3 min read
How to Get Index of a Substring in Python?
To get index of a substring within a Python string can be done using several methods such as str.find(), str.index(), and even regular expressions. Each approach has its own use case depending on the requirements. Letâs explore how to efficiently get the index of a substring. The simplest way to get
2 min read
Python | Ways to remove numeric digits from given string
In Python, we can remove numeric digits from a string in multiple ways. For example, if we have a string like "Geeks123" and we want to remove the numbers to get "Geeks", we can use different methods to accomplish this. We can do this by using techniques such as list comprehension, regular expressio
3 min read
Python - Get all substrings of given string
A substring is any contiguous sequence of characters within the string. We'll discuss various methods to extract this substring from a given string by using a simple approach. Using List Comprehension :List comprehension offers a concise way to create lists by applying an expression to each element
3 min read