473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Chopping off spaces at both ends

Hi

I am a newbie to python and am using it to interface some of my lab
equipment.

How does one get rid of spaces at both ends of a string ? A little like the
trim() intrinsic in fortran 95.

One of my instruments is returning a string that has one or more blanks in
it, and that is complicating string matching tests in one of my functions.

Thanks.
Aug 7 '05 #1
3 1088
Use the strip() method.

Example:
"\t abc \n".strip()

"abc"

Variants are lstrip() and rstrip().

Regards,
Kay

Aug 7 '05 #2
On 7 Aug 2005 10:14:33 -0700, "Kay Schluehr" <ka**********@gmx.net> wrote:
Use the strip() method.

Example:
"\t abc \n".strip()"abc"

Variants are lstrip() and rstrip().


and also occasionally useful:
'abc123cab'.strip('bca')

'123'

I.e., a strip argument as an unordered set of characters that
causes stripping so long as characters at the end(s) of the
string being stripped are found that are in the set.

Regards,
Bengt Richter
Aug 7 '05 #3
Madhusudan Singh <sp**************@spam.invalid> wrote:

I am a newbie to python and am using it to interface some of my lab
equipment.

How does one get rid of spaces at both ends of a string ? A little like the
trim() intrinsic in fortran 95.

One of my instruments is returning a string that has one or more blanks in
it, and that is complicating string matching tests in one of my functions.

a = ' abc '
print a.strip()

'abc'
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Aug 9 '05 #4

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

Similar topics

9
by: hokiegal99 | last post by:
This script works as I expect, except for the last section. I want the last section to actually remove all spaces from the front and/or end of filenames. For example, a file that was named " test ...
4
by: Marc Durufle | last post by:
I have a string like that " Vertices " and i want to obtain "Vertices" I would want to know if there is a simple way to put off spaces on a string, thank you -- Marc Durufle Inria...
7
by: Eric Johannsen | last post by:
Hi, My C# code is calling VB6 code, which expects all (fixed-length) strings to be padded with spaces. The strings are contained with a struct, something like this (attributes to simulate...
9
by: Andy B | last post by:
If I bought one of these boxes/OS combos as a postgresql database server, would postgresql be able to make the best use of it with a huge (e.g. 40GB) database? Box: HP ProLiant DL585, with ...
22
by: rtilley | last post by:
# Spaces are present before and after the XXX filename = ' XXX ' new_filename = filename.strip() if new_filename != filename: print filename Macs allow these spaces in file and folder...
15
by: DanielJohnson | last post by:
I am writing a program in which I am removing all the spaces from the string. I thought that I could do it two ways. One was parsing the string character by character and copying onto another...
7
by: Paul Furman | last post by:
mysql_real_escape_string() is apparently chopping off anything that follows a quote when I grab the data & put it in a form for editing. Sorry if I'm not explaining this properly, I'm pretty...
2
by: djm | last post by:
hello everyone. ive a string with a blank space at the start for eg: string str=" c++ rocks!"; how can i chop of that blank space of the string and display? thanks
5
by: brian.j.parker | last post by:
Hey all, I've noticed an obscure little quirk: it appears that if you use a login with trailing spaces on the name, SYSTEM_USER automatically trims those trailing spaces in SQL Server 2000, but not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.