473,608 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

whitespace

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

import string
"a b c".translate(st ring.maketrans( "", ""), string.whitespa ce) 'abc'


Peter
Jul 18 '05 #2
Quoting RR (rr**@cornell.e du):
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.e du> 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
'hellothereworl d'

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.e du> 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
90001
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 to ignore all whitespace in my subject string? There is a global modifier to ignore all spaces in the pattern, but I couldn't find one for ignoring spaces in the subject string. Do I really have to either do a preg_replace to remove all whitespace or stick a lot of \s* into my search pattern? ...
2
2146
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 notional DocBook XML document: <para> This is a longer paragraph. With <wordasword>longer</wordasword> I mean that it contains more than one sentence.
2
2019
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 (http://xml.apache.org/xerces-j/apiDocs/org/apache/xerces/dom/TextImpl.html#isIgnorableWhitespace()). ; I guess when they refer to "ignorable whitespace" in Java we may interpret that as an "insignificant whitespace" in .NET. Am I correct to say that? So, I need to manually convert some Java code to C#,...
2
1939
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 (http://xml.apache.org/xerces-j/apiDocs/org/apache/xerces/dom/TextImpl.html#isIgnorableWhitespace()). I guess when they refer to "ignorable whitespace" in Java we may interpret that as an "insignificant whitespace" in .NET. Am I correct to say that? So, I need to manually convert some Java code to C#,...
0
2236
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 very well. One of the common regex's that I use follows: ValidationExpression = "^\d{0,3}(\.\d{0,4})?$" The purpose of this one is to validate that numeric values input follow the syntax 999.9999. This works well. But, one thing that I have never tested previously (which has now been uncovered...
3
2619
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 the items in the list that are only whitespace (can be one or more characters of whitespace) and plan to use string.strip on those items that are not only whitespace (to remove any whitespace from front or back of items). What kind of efficient test can I use to obtain only list items returned...
56
3530
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 a mythical caste of tasteless heathens that "we" have invented. It just sounds like so much trivial nitpickery that it's hard to believe it's as common as we've come to believe.
9
2553
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 webservice? This has been discussed before, but no one has ever come up with a good answer to what seems like such a common question. ...
5
3892
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> </a> When I execute an XSL transform, the resulting HTML looks like this:
13
27950
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 into its component words, while the latter two return the whole sentence entact. import string from string import whitespace mytext = "The quick brown fox jumped over the lazy dog.\n" print mytext.split()
0
8059
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8470
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8145
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6815
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5475
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3960
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.