Python – Extract String till Numeric

Given a string, extract all its content till first appearance of numeric character.

Input : test_str = “geeksforgeeks7 is best”
Output : geeksforgeeks
Explanation : All characters before 7 are extracted.

Input : test_str = “2geeksforgeeks7 is best”
Output : “”
Explanation : No character extracted as 1st letter is numeric.

Method #1 : Using isdigit() + index() + loop

The combination of above functions can be used to solve this problem. In this, we check for first occurrence of numeric using isdigit() and index() is used to get required index till which content needs to be extracted.



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Extract String till Numeric
# Using isdigit() + index() + loop
  
# initializing string
test_str = "geeks4geeks is best"
  
# printing original string
print("The original string is : " + str(test_str))
  
# loop to iterating characters
temp = 0
for chr in test_str:
      
  # checking if character is numeric,
  # saving index
  if chr.isdigit():
     temp = test_str.index(chr)
  
# printing result 
print("Extracted String : " + str(test_str[0 : temp])) 

chevron_right


Output

The original string is : geeks4geeks is best
Extracted String : geeks

Method #2 : Using regex()

This is yet another way in which this task can be performed. Using appropriate regex(), one can get content before possible numerics.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Extract String till Numeric
# Using regex()
import re
  
# initializing string
test_str = "geeks4geeks is best"
  
# printing original string
print("The original string is : " + str(test_str))
  
# regex to get all elements before numerics
res = re.findall('([a-zA-Z ]*)\d*.*', test_str)
  
# printing result 
print("Extracted String : " + str(res[0])) 

chevron_right


Output

The original string is : geeks4geeks is best
Extracted String : geeks



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.