Hal
Hal

Reputation: 372

How do I split sentence on 'and'?

I am trying to split my sentence on 'and' but some of the results looks like this

My code

string = 'I am handling it because it is difficult and confusing'

string.split('and')

Results

['I am h', 'ling it because it is difficult ', ' confusing']

I am trying to get this. How do I do it?

['I am handling it because it is difficult ', ' confusing']

Upvotes: 2

Views: 197

Answers (1)

Leo
Leo

Reputation: 317

Try doing

string.split(" and ")

It will only pick the word. But if you need spaces, this function/loop will do(tested):

add_spaces(x): 
    x[0] += ' '
    for i in range(1, len(x) - 1):
        x[i] = ' ' + x[i]
        x[i] += ' '
    x[-1] = ' ' +  x[-1]

Upvotes: 1

Related Questions