whitespace

T

Tim Rowe

Is there an easy way in python to remove whitespace from a string?

From the ends or all of it?

From the ends,
" hello ".strip()
gives "hello"

There are lstrip and rstrip if you only want to strip from one end.

To get rid of all whitespace I'd probably do:

import string
string.join(" hello there world ".split(), "")

which gives
'hellothereworld'

Though there may be an easier way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top