How to Remove Whitespace from a String in Python

If you need to clean up text in Python, there are a few common ways to remove whitespace from a string.

This page shows you how to:

  • Remove spaces at the beginning and end
  • Remove all normal spaces
  • Remove tabs and newline characters too
  • Choose the right method for the result you want

Quick fix #

text = "  hello world  "
print(text.strip())           # hello world
print(text.replace(" ", ""))  # helloworld

Use strip() to remove whitespace at the start and end.

Use replace() to remove normal space characters everywhere.

What this page helps you do #

  • Remove spaces at the beginning and end of a string
  • Remove all normal spaces from a string
  • Remove tabs and newline characters when needed
  • Choose the right method for your goal

Use strip() to remove whitespace from both ends #

The strip() method removes whitespace from the start and end of a string.

It does not remove spaces in the middle.

This is useful when you want to clean input from a user or text read from a file.

text = "  hello world  "
cleaned = text.strip()

print(cleaned)

Output:

hello world

Notice that the space between hello and world stays there.

You can also use:

  • lstrip() to remove whitespace from the left side only
  • rstrip() to remove whitespace from the right side only

Example:

text = "  hello world  "

print(text.lstrip())
print(text.rstrip())

Output:

hello world  
  hello world

If you want more detail, see the strip() string method.

Use replace() to remove all space characters #

If you want to remove all normal spaces from a string, use replace(" ", "").

text = "h e l l o   w o r l d"
cleaned = text.replace(" ", "")

print(cleaned)

Output:

helloworld

This works well when you want to remove visible space characters everywhere in the text.

Important: this removes only the normal space character " ".

It does not remove:

  • tabs (\t)
  • newline characters (\n)

Example:

text = "hello\tworld\npython code"
cleaned = text.replace(" ", "")

print(repr(cleaned))

Output:

'hello\tworld\npythoncode'

The tab and newline are still there.

To learn more, see the replace() string method or this guide on how to replace text in a string in Python.

Use split() and join() to remove all kinds of whitespace #

If your text may contain spaces, tabs, or newlines, a useful pattern is:

"".join(text.split())

Here is how it works:

  • split() with no argument splits the string on any whitespace
  • Whitespace includes spaces, tabs, and newlines
  • join() puts the pieces back together with no separator

Example:

text = "  hello\tworld \n python  "
cleaned = "".join(text.split())

print(repr(cleaned))

Output:

'helloworldpython'

This removes all whitespace characters, not just normal spaces.

Here is another example:

text = "one   two\tthree\nfour"
parts = text.split()
cleaned = "".join(parts)

print(parts)
print(cleaned)

Output:

['one', 'two', 'three', 'four']
onetwothreefour

This approach is helpful for messy text copied from forms, files, or multiple lines.

If you are new to these methods, see:

Pick the right approach #

Use the method that matches your goal:

  • Use strip() when you only want to trim the start and end
  • Use replace(" ", "") when you want to remove normal spaces only
  • Use "".join(text.split()) when the text may contain tabs or line breaks too

Example comparison:

text = "  hello\tworld \n "

print(repr(text.strip()))
print(repr(text.replace(" ", "")))
print(repr("".join(text.split())))

Output:

'hello\tworld'
'hello\tworld\n'
'helloworld'

Each method gives a different result.

Things beginners often get wrong #

A few common problems cause confusion here.

Strings are immutable #

In Python, strings cannot be changed in place.

String methods return a new string.

This means:

text = "  hello  "
text.strip()

print(repr(text))

Output:

'  hello  '

The original value did not change.

You need to save the result:

text = "  hello  "
text = text.strip()

print(repr(text))

Output:

'hello'

strip() does not remove spaces in the middle #

This code:

text = "  hello world  "
print(text.strip())

Output:

hello world

The middle space is still there, because strip() only affects the ends.

replace(" ", "") does not remove every kind of whitespace #

If the string contains tabs or newlines, replace(" ", "") is not enough.

text = "hello\tworld\npython"
print(repr(text.replace(" ", "")))

Output:

'hello\tworld\npython'

Use split() and join() if you need to remove all whitespace types.

Common causes #

If your code is not giving the result you expect, these are common reasons:

  • Using strip() and expecting middle spaces to disappear
  • Using replace(" ", "") when the string contains tabs or newlines
  • Forgetting to save the result in a new variable or back to the same variable
  • Not checking what kind of whitespace is actually in the string

Debugging tips #

These quick checks help you see what is really in the string:

print(repr(text))
print(text.strip())
print(text.replace(" ", ""))
print("".join(text.split()))
print(len(text), len(text.strip()))

Why these help:

  • repr(text) shows hidden characters like \t and \n
  • strip() lets you test trimming the ends
  • replace(" ", "") shows what happens when only spaces are removed
  • "".join(text.split()) shows the result when all whitespace is removed
  • len() helps you compare before and after

FAQ #

How do I remove spaces only from the start and end of a string? #

Use strip(). It removes whitespace from both ends of the string.

How do I remove all spaces from a string in Python? #

Use replace(" ", "") to remove normal spaces everywhere.

How do I remove tabs and newline characters too? #

Use "" .join(text.split()) to remove all whitespace characters:

text = "a\tb\nc d"
cleaned = "".join(text.split())
print(cleaned)

Output:

abcd

Why did my original string not change? #

String methods return a new string. Assign the result back to a variable.

text = "  hello  "
text = text.strip()

See also #

Press Esc to close