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'

2 Answers2

0

you don't need insert ' ' for split.

try this:

var = "this is a string"
var = var.replace('a' , '').split()
print(var)
I'mahdi
  • 11,310
  • 3
  • 17
  • 23
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
  • 67,685
  • 6
  • 55
  • 77