ovod
ovod

Reputation: 1178

"\t" symbols remain after stripping

I have a string and some code to strip it:

def break_words(stuff):
words = stuff.split(' ')
return sorted(words)
sentence = 'All god'+"\t"+'things come to those who weight.'
print sentence#works as expected
words = break_words(sentence)
print words

sentence is printed as expected (without the \t symbol); but words is printed as:

['All', 'come', 'god\tthings', 'those', 'to', 'weight.', 'who']

How can I remove \t from the list?

Upvotes: 0

Views: 117

Answers (2)

Henrik Andersson
Henrik Andersson

Reputation: 47212

sentence = 'All god'+"\t"+'things come to those who weight.'
words = sentence.expandtabs().split(' ')
words = sorted(words)
>> ['All', 'come', 'god', 'things', 'those', 'to', 'weight.', 'who']

or you could wrap it in sorted() directly

words = sorted(sentence.expandtabs().split(' '))
>> ['All', 'come', 'god', 'things', 'those', 'to', 'weight.', 'who']

Upvotes: 1

Dinever
Dinever

Reputation: 690

You can use .replace('\t',' ') or .expandtabs()

Then all the new tab characters entered will be changed to spaces.

Try This

def break_words(stuff):
    words = stuff.replace('\t','').split(' ')
    return sorted(words)

sentence = 'All god'+"\t"+'things come to those who weight.'
print sentence#works as expected
words = break_words(sentence)
print w

Output:

All god things come to those who weight.
['All', 'come', 'godthings', 'those', 'to', 'weight.', 'who']

Or this

def break_words(stuff):
    words = stuff.replace('\t',' ').split(' ')
    return sorted(words)

sentence = 'All god'+"\t"+'things come to those who weight.'
print sentence#works as expected
words = break_words(sentence)
print words

Output:

All god things come to those who weight.
['All', 'come', 'god', 'things', 'those', 'to', 'weight.', 'who']

Best Regards :)

Upvotes: 1

Related Questions