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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


Output:



initial string :  123abcjw abceddfgh
first string result:  123
second string result:

Method #2: Using takewhile

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


Output:

initial string :  123abcjw abceddfgh
first string result:  123
second string result:

 
Method #3: Using re.sub

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


Output:

initial string :  123abcjw abceddfgh
first string result:  123
second string result:

 

Using Method #4: Using re.findall

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


Output:

initial string :  123abcjw abceddfgh
first string result:  123
second string result:


My Personal Notes arrow_drop_up

Programming freaktech Enthusiast and have interest in learning new upcoming technologies

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.