-1

Can you delete keywords in a string?

I tried using:

if "keyword" in var:
  print(var - "keyword")

But it didn't work, and it let out out this error message:

TypeError: unsupported operand type(s) for -: 'str' and 'str'

I'm probably doing this incorrectly, so is there another way of doing this?

Thanks.

jort57
  • 76
  • 1
  • 10

1 Answers1

2

you can use replace() for this

str = "hello world"
print(str.replace("world",""))

#output
>>> hello 

so with your implementation:

if keyword in var: #keyword as variable
    var = var.replace(keyword, "")
Ironkey
  • 2,568
  • 1
  • 8
  • 30