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
# Python code to demonstrate # to get numeric prefix in string # if present # initialising string ini_string = "123abcjw" ini_string2 = "abceddfgh" # printing string and its length print ( "initial string : " , ini_string, ini_string2) # code to find numeric prefix in string res1 = ' '.join(c for c in ini_string if c in ' 0123456789 ') res2 = ' '.join(c for c in ini_string2 if c in ' 0123456789 ') # printing resultant string print ( "first string result: " , str (res1)) print ( "second string result: " , str (res2)) |
chevron_right
filter_none
Output:
initial string : 123abcjw abceddfgh first string result: 123 second string result:
Method #2: Using takewhile
# Python code to demonstrate # to get numeric prefix in string # if present from itertools import takewhile # initialising string ini_string = "123abcjw" ini_string2 = "abceddfgh" # printing string and its length print ( "initial string : " , ini_string, ini_string2) # code to find numeric prefix in string res1 = ''.join(takewhile( str .isdigit, ini_string)) res2 = ''.join(takewhile( str .isdigit, ini_string2)) # printing resultant string print ( "first string result: " , res1) print ( "second string result: " , res2) |
chevron_right
filter_none
Output:
initial string : 123abcjw abceddfgh first string result: 123 second string result:
Method #3: Using re.sub
# Python code to demonstrate # to get numeric prefix in string # if present import re # initialising string ini_string = "123abcjw" ini_string2 = "abceddfgh" # printing string and its length print ( "initial string : " , ini_string, ini_string2) # code to find numeric prefix in string res1 = re.sub( '\D.*' , '', ini_string) res2 = re.sub( '\D.*' , '', ini_string2) # printing resultant string print ( "first string result: " , str (res1)) print ( "second string result: " , str (res2)) |
chevron_right
filter_none
Output:
initial string : 123abcjw abceddfgh first string result: 123 second string result:
Using Method #4: Using re.findall
# Python code to demonstrate # to get numeric prefix in string # if present import re # initialising string ini_string = "123abcjw" ini_string2 = "abceddfgh" # printing string and its length print ( "initial string : " , ini_string, ini_string2) # code to find numeric prefix in string res1 = ' '.join(re.findall(' \d + ', ini_string)) res2 = ' '.join(re.findall(' \d + ', ini_string2)) # printing resultant string print ( "first string result: " , str (res1)) print ( "second string result: " , str (res2)) |
chevron_right
filter_none
Output:
initial string : 123abcjw abceddfgh first string result: 123 second string result:
Recommended Posts:
- Python | Get numeric prefix of given string
- Python | Check if given string is numeric or not
- Python | Check Numeric Suffix in String
- Python | Ways to remove numeric digits from given string
- Python Regex to extract maximum numeric value from a string
- Python | Convert numeric String to integers in mixed List
- Python program to print the substrings that are prefix of the given string
- Python | Sort numeric strings in a list
- Python | Add only numeric values present in a list
- Find the longest common prefix between two strings after performing swaps on second string
- Python | Prefix sum list
- Python | Prefix key match in dictionary
- Python | Prefix Sum Subarray till False value
- Python | Prefix extraction before specific character
- Prefix sum array in Python using accumulate function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.