472,353 Members | 1,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

whitespace

RR
Is there an easy way in python to remove whitespace from a string?
Jul 18 '05 #1
5 51847
RR wrote:
Is there an easy way in python to remove whitespace from a string?

import string
"a b c".translate(string.maketrans("", ""), string.whitespace) 'abc'


Peter
Jul 18 '05 #2
Quoting RR (rr**@cornell.edu):
Is there an easy way in python to remove whitespace from a string?
Depending on what you mean by "remove whitespace":
mystring = " a b c d e f g "
mystring.strip() 'a b c d e f g' import re
re.sub("\s+", "", mystring)

'abcdefg'

--
http://mail.python.org/mailman/listinfo/python-list


--
Geoff Gerrietts "Whenever people agree with me I always
<geoff at gerrietts net> feel I must be wrong." --Oscar Wilde

Jul 18 '05 #3
On Fri, 19 Sep 2003 19:35:54 -0400, "RR" <rr**@cornell.edu> wrote:
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.


Jul 18 '05 #4
RR wrote:

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

' string with whitespace '.strip()

'string with whitespace'

-Peter
Jul 18 '05 #5
"RR" <rr**@cornell.edu> wrote in message news:<pa****************************@cornell.edu>. ..
Is there an easy way in python to remove whitespace from a string?


' hello world '.replace(' ', '')

'helloworld'

Hung Jung
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Thomas Mlynarczyk | last post by:
Which is the simplest way to remove all whitespace from a string? Is there a simpler method than a regex replace? Or how can I tell a regex pattern...
2
by: Wolfgang Jeltsch | last post by:
Hello, it is often convenient to insert whitespace into an XML document in order to format it nicely. For example, take this snippet of a...
2
by: Carlitos | last post by:
Hi there, A class in Xerces J-API (Java) called TextImpl contains a property that returns whether the text is ignorable whitespace...
2
by: Carlitos | last post by:
Hi there, A class in Xerces J-API (Java) called TextImpl contains a property that returns whether the text is ignorable whitespace...
0
by: Shan Plourde | last post by:
Hi everyone, I have been using various regular expressions with the ASP.NET RegularExpressionValidator for quite some time. In general it works...
3
by: David Pratt | last post by:
Hi. I am splitting a string on a non whitespace character. One or more whitespace characters can be returned as items in the list. I do not want...
56
by: infidel | last post by:
Where are they-who-hate-us-for-our-whitespace? Are "they" really that stupid/petty? Are "they" really out there at all? "They" almost sound like...
9
by: amattie | last post by:
Does anyone have any idea on how I can strip the extra whitespace in the XML that shows up when I receive a response from an ASP.NET 2.0...
5
by: John Gordon | last post by:
My XSLT files have many occurrences of this general pattern: <a> <xsl:attribute name="href"> <xsl:value-of select="xyz" /> </xsl:attribute>...
13
by: Chaim Krause | last post by:
I am unable to figure out why the first two statements work as I expect them to and the next two do not. Namely, the first two spit the sentence...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.