0
var = "this is a string"
var = var.replace('a' , '')
var = var.split(' ')
print(var)

The above code displays:

['this','is','','string']

I want it to display:

['this','is','string']

How do I do that?

What have I tried: Assigning none to the Argument 2, but It raises an error 'TypeError: replace() argument 2 must be str, not None'

1 Answers1

0

You might not want to replace "a" within words, so you better filter after the split:

var = "this is a string"

var = [w for w in var.split() if w != "a"]
user2390182
  • 72,016
  • 6
  • 67
  • 89