Re: remove the last character or the newline character?
- From: Bruno Desthuilliers <onurb@xxxxxxxxxxx>
- Date: Thu, 28 Sep 2006 16:26:04 +0200
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@xxxxxxxxxxx'.split('@')])"
.
- References:
- remove the last character or the newline character?
- From: Daniel Mark
- remove the last character or the newline character?
- Prev by Date: Re: remove the last character or the newline character?
- Next by Date: best way to get data into a new instance?
- Previous by thread: Re: remove the last character or the newline character?
- Next by thread: Re: remove the last character or the newline character?
- Index(es):
Relevant Pages
|