Goodbying Spaces

Luis Zarrabeitia kyrie at uh.cu
Fri Apr 25 17:29:29 EDT 2008


Whats the result of using id.strip()?:

In [1]: " asdfasdf   ".strip()
Out[1]: 'asdfasdf'

It should work, I guess...

Btw, you can write your code without using len in a cleaner way:

try:
  if id[0] == ' ':
    id = id[1:]   # Note this slice notation...
except:
  pass

try:
  if id[-1] == ' ':   # id[-1] would be the last character
    id = id[:-1]      # Again, a slice with only one argument
except:
  pass

Cheers,

K.

On Friday 25 April 2008 05:01:31 pm Victor Subervi wrote:
>  Hi;
> Whenever I call a field from the preceeding form using cgi.FieldStorage() I
> get a space on either side. I end up writing code like this to get rid of
> that space:
>
> try:
>
>   if id[0] == ' ':
>
>   id = id[1:len(id)]
>
> except:
>
>   pass
>
> try:
>
>   if id[len(id) - 1] == ' ':
>
>   id = id[0:len(id) - 1]
>
> except:
>
>   pass
> which is a nuisance. Is there a better way to do this? I have tried
> id.strip() with no luck. What do?
> TIA,
> Victor



-- 
Luis Zarrabeitia (aka Kyrie)
Fac. de Matemática y Computación, UH.
http://profesores.matcom.uh.cu/~kyrie



More information about the Python-list mailing list