-1

I am trying to find 2 re.sub() lines. The first one changes any number of spaces and/or tab characters in any order into a space. (Maybe this should be 2 separate ones?) The second one changes anything that is not a letter or a space into an empty string. I included what I know so far for each one below

re.sub("?"," ",word)
re.sub("?","",word)

What should be put where the question marks are? Thanks.

Steven
  • 6,053
  • 2
  • 16
  • 28

3 Answers3

1

https://docs.python.org/3/library/re.html

  • \s+ for all whitespace, but watch out for the details
  • [ \t]+ if you only want tabs & spaces
Andrew
  • 8,322
  • 2
  • 47
  • 70
0
import re

word = "hello    this is \ta\ttest"
result = re.sub(r'\s+', " ", word) 

print(result)  # prints: hello this is a test
Miguel Prz
  • 13,718
  • 29
  • 42
0

Code:

re.sub('\s+', ' ', word)
Aaj Kaal
  • 1,205
  • 1
  • 9
  • 8