2

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']
Hal
  • 372
  • 4
  • 13

1 Answers1

1

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]
Leo
  • 319
  • 3
  • 18