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
# 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 = 0for 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])) |
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
# 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])) |
The original string is : geeks4geeks is best Extracted String : geeks
Recommended Posts:
- Python program to Extract string till first Non-Alphanumeric character
- Python Regex to extract maximum numeric value from a string
- Python - Extract Tuples with all Numeric Strings
- Python | Repeat String till K
- Python - String till Substring
- Python | Combinations of elements till size N in list
- Python | Prefix Sum Subarray till False value
- Python | Getting sublist element till N
- Python | Get elements till particular element in list
- Python | Count the elements till first tuple
- Python | Print Alphabets till N
- Python - Maximum element till K value
- Python | Get Kth element till N
- Python | Values till False element
- Python | Multiplication till Null value
- Python | Values Frequency till Maximum K
- Python - Elements Product till K value
- Python - Remove characters till K element
- Python - All Possible unique K size combinations till N
- Python - Slice till K dictionary value lists
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.

