string.rstrip

Erik Max Francis max at alcyone.com
Mon Sep 22 18:26:17 EDT 2003


Hank wrote:

> I have this problem with string.rstrip
> 
> Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on
> win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import string
> >>> mystr = "test.txt"
> >>> mystr = string.rstrip(mystr, ".txt")
> >>> print mystr
> tes
> 
> The last 't' was stripped.

What you mean is the result is 'tes', which is correct.

> Is this fixed or documented anywhere? it
> works for other cases, but this one seems to give me a weird result.

You're using .rstrip incorrectly.  The second argument to string.rstrip
is a list of characters, any one of which will be stripped if they are
at the end of the string.  So you're not saying, "remove .txt from the
end of the string," you're saying, "remove any of the characters ., t,
or x from the end of the string until you don't find anymore."  Since
you give it test.txt, that means it strips everything up to the s, since
they're all one of those characters.

It's documented in the library reference as well as the interpreter help
facility.

	http://www.python.org/doc/current/lib/module-string.html
	help(''.rstrip)

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ Grab a club and join the chorus / Evolution is a state of mind
\__/  Oleta Adams




More information about the Python-list mailing list