remove the last character or the newline character?

John Salerno johnjsal at NOSPAMgmail.com
Thu Sep 28 16:22:30 CEST 2006


Daniel Mark wrote:

> Question one:
> Does python provide any function that can remove the last character of
> a string?
> I don't know whether or not the method I used is efficient

Doing fileName[-1] lets you access the last character, but I'm not sure 
about removing it since strings are immutable. It would still probably 
be something like you did.

> Question two:
> Does python provide any function that can remove the newline character
> from a string if it exists?

 >>> fileName = 'Perfect Setup.txt\n'
 >>> fileName.strip()
'Perfect Setup.txt'
 >>>

Or you can use lstrip() or rstrip() to remove just the left or right side.



More information about the Python-list mailing list