remove the last character or the newline character?
Bruno Desthuilliers
onurb at xiludom.gro
Thu Sep 28 10:26:04 EDT 2006
Daniel Mark wrote:
> Hello all:
>
> I have the following snippet:
>
> In [1]: fileName = 'Perfect Setup.txt\n'
> In [2]: fileName = fileName[0:len(fileName)-1)] # remove the '\n'
fileName = fileName.rstrip('\n')
or just a plain:
fileName = fileName.strip()
> character
> In [3]: fileName
> Out[3]: 'Perfect Setup.txt'
>
> Question one:
> Does python provide any function that can remove the last character of
> a string?
Not directly, since Python strings are immutable objects. If you want a
copy of the string without the last char *whatever it is*, you can just use
somestr = somestr[0:-1]
But in your situation, it's usually safer to use [lr]?strip()
>
> Question two:
> Does python provide any function that can remove the newline character
> from a string if it exists?
Here again, you cannot *remove* anything from a string - you can just
have a modified copy copy of the string. (NB : answer is just above :
use str.strip())
HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"
More information about the Python-list
mailing list