How do i remove a tab from a string in python?

I am trying to remove all spaces/tabs/newlines in python 2.7 on Linux.

I wrote this, that should do the job:

myString="I want to Remove all white \t spaces, new lines \n and tabs \t"
myString = myString.strip(' \n\t')
print myString

output:

I want to Remove all white   spaces, new lines 
 and tabs

It seems like a simple thing to do, yet I am missing here something. Should I be importing something?

How do i remove a tab from a string in python?

asked May 22, 2012 at 22:37

3

Use str.split([sep[, maxsplit]]) with no sep or sep=None:

From docs:

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.

Demo:

>>> myString.split()
['I', 'want', 'to', 'Remove', 'all', 'white', 'spaces,', 'new', 'lines', 'and', 'tabs']

Use str.join on the returned list to get this output:

>>> ' '.join(myString.split())
'I want to Remove all white spaces, new lines and tabs'

answered May 22, 2012 at 22:42

How do i remove a tab from a string in python?

Ashwini ChaudharyAshwini Chaudhary

236k55 gold badges443 silver badges495 bronze badges

0

If you want to remove multiple whitespace items and replace them with single spaces, the easiest way is with a regexp like this:

>>> import re
>>> myString="I want to Remove all white \t spaces, new lines \n and tabs \t"
>>> re.sub('\s+',' ',myString)
'I want to Remove all white spaces, new lines and tabs '

You can then remove the trailing space with .strip() if you want to.

answered May 22, 2012 at 22:40

MattHMattH

36k11 gold badges81 silver badges84 bronze badges

Use the re library

import re
myString = "I want to Remove all white \t spaces, new lines \n and tabs \t"
myString = re.sub(r"[\n\t\s]*", "", myString)
print myString

Output:

IwanttoRemoveallwhitespaces,newlinesandtabs

answered Dec 30, 2017 at 16:36

How do i remove a tab from a string in python?

skt7skt7

1,1398 silver badges20 bronze badges

1

This will only remove the tab, newlines, spaces and nothing else.

import re
myString = "I want to Remove all white \t spaces, new lines \n and tabs \t"
output   = re.sub(r"[\n\t\s]*", "", myString)

OUTPUT:

IwantoRemoveallwhiespaces,newlinesandtabs

Good day!

Jesuisme

1,6351 gold badge31 silver badges39 bronze badges

answered Dec 12, 2017 at 9:49

How do i remove a tab from a string in python?

The Gr8 AdakronThe Gr8 Adakron

1,1621 gold badge12 silver badges14 bronze badges

1

import re

mystr = "I want to Remove all white \t spaces, new lines \n and tabs \t"
print re.sub(r"\W", "", mystr)

Output : IwanttoRemoveallwhitespacesnewlinesandtabs

answered Dec 31, 2012 at 11:32

Manish MulaniManish Mulani

6,8779 gold badges41 silver badges45 bronze badges

1

The above solutions suggesting the use of regex aren't ideal because this is such a small task and regex requires more resource overhead than the simplicity of the task justifies.

Here's what I do:

myString = myString.replace(' ', '').replace('\t', '').replace('\n', '')

or if you had a bunch of things to remove such that a single line solution would be gratuitously long:

removal_list = [' ', '\t', '\n']
for s in removal_list:
  myString = myString.replace(s, '')

answered May 1, 2019 at 20:09

How do i remove a tab from a string in python?

rosstripirosstripi

5531 gold badge9 silver badges19 bronze badges

How about a one-liner using a list comprehension within join?

>>> foobar = "aaa bbb\t\t\tccc\nddd"
>>> print(foobar)
aaa bbb                 ccc
ddd

>>> print(''.join([c for c in foobar if c not in [' ', '\t', '\n']]))
aaabbbcccddd

answered Sep 30, 2020 at 14:11

How do i remove a tab from a string in python?

sqqqrlysqqqrly

8471 gold badge7 silver badges10 bronze badges

Since there is not anything else that was more intricate, I wanted to share this as it helped me out.

This is what I originally used:

import requests
import re

url = 'https://stackoverflow.com/questions/10711116/strip-spaces-tabs-newlines-python' # noqa
headers = {'user-agent': 'my-app/0.0.1'}
r = requests.get(url, headers=headers)
print("{}".format(r.content))

Undesired Result:

b'\r\n\r\n\r\n    \r\n\r\n    \r\n\r\n        string - Strip spaces/tabs/newlines - python - Stack Overflow\r\n        

This is what I changed it to:

import requests
import re

url = 'https://stackoverflow.com/questions/10711116/strip-spaces-tabs-newlines-python' # noqa
headers = {'user-agent': 'my-app/0.0.1'}
r = requests.get(url, headers=headers)
regex = r'\s+'
print("CNT: {}".format(re.sub(regex, " ", r.content.decode('utf-8'))))

Desired Result:

   string - Strip spaces/tabs/newlines - python - Stack Overflow

The precise regex that @MattH had mentioned, was what worked for me in fitting it into my code. Thanks!

Note: This is python3

answered May 15, 2019 at 6:54

JayRizzoJayRizzo

2,7843 gold badges31 silver badges42 bronze badges

How do you delete a tab in Python?

Python Trim String.
strip(): returns a new string after removing any leading and trailing whitespaces including tabs ( \t )..
rstrip(): returns a new string with trailing whitespace removed. ... .
lstrip(): returns a new string with leading whitespace removed, or removing whitespaces from the “left” side of the string..

How do you remove newlines and tabs from a string in Python?

Use the strip() Function to Remove a Newline Character From the String in Python. The strip() function is used to remove both trailing and leading newlines from the string that it is being operated on. It also removes the whitespaces on both sides of the string.

Does Python strip remove tabs?

Trim Methods - strip() In Python, the stripping methods are capable of removing leading and trailing spaces and specific characters. The leading and trailing spaces, include blanks, tabs ( \t ), carriage returns ( \r , \n ) and the other lesser-known whitespace characters that can be found here.

How do I remove tabs from text?

To clear a tab stop.
Go to Home and select the Paragraph dialog launcher ..
Select Tabs..
Do one of the following: Select a tab stop and select Clear. Select Clear All to remove all tab stops..
Select OK..