0

Hi all I was wondering if it is possible in python to extract words from a string before a number.

For Example:

Hi my name is hazza 50 test test test

Hi hazza 60 test test test

hazza 50 test test test

I want to get the words before the number not after if it is possible.

Hi my name is hazza

Hi hazza

hazza

Regards Hazza

1
  • 1
    Absolutely, there are plenty of ways to do this. You might want to look into regular expressions. If there will always be a space before and after the number you could also use str.split() and str.isdigit() along with a for loop. Commented Jun 19, 2020 at 17:14

4 Answers 4

3

Regex will do

import re

strings = '''
Hi my name is hazza 50 test test test

Hi hazza 60 test test test

hazza 50 test test test

hazza test test test
'''

for s in re.findall('([a-zA-Z ]*)\d*.*',strings):
    print(s)

Gives

Hi my name is hazza 

Hi hazza 

hazza 

hazza test test test
Sign up to request clarification or add additional context in comments.

3 Comments

Quick question, I've been using this to run through my data but I have encountered a problem where some entries don't have a number. Is there any way round this. I have tried using an elif statment to cut the rest of the words if there is a new line.
The newly edited code should work, try it out @TheAmazingHAzza
Thanks a lot for the newly edited answer that has helped me a lot. Thanks again!
0
is_digit = False
str = "Hi my name is hazza 50 test test test"
r = 0

for c in str:
  if c.isdigit():
     # is_digit = True
     r = str.index(c)

print(str[0:r-2])

r is index of 5 r-2 because you want the string without that space before 50

read : https://www.learnpython.org/

Comments

0
s = "Hi my name is hazza 50 test test test"
result = ""
for i, char in enumerate(s):
    if char.isdigit():
        result = s[:i]
        break
print(result)

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
0

This implementation will allow you to extract all sets of words before each number within your string.

s = '50 Hi hazza 60 test test 70 test'
# Split string on spaces
split = s.split()
# Instantiate list to hold words
words = []
curr_string = ''
for string in split:
    # Check if string is numeric value
    if string.isnumeric():
        # Catch edge case where string starts with number
        if curr_string != '':
            # Add curr_string to words list -- remove trailing whitespace
            words.append(curr_string.strip())
            curr_string = ''
    else:
        # If string not numeric, add to curr_string
        curr_string += string + ' '

print(words)

Output: ['Hi hazza', 'test test']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.