Re: remove the last character or the newline character?
- From: Georg Brandl <g.brandl-nospam@xxxxxxx>
- Date: Thu, 28 Sep 2006 16:23:05 +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'
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?
I don't know whether or not the method I used is efficient
fileName = fileName[:-1]
Question two:
Does python provide any function that can remove the newline character
from a string if it exists?
fileName = fileName.rstrip("\n")
though this will remove more than one newline if present. If you only want
to remove one newline, use
if fileName[-1:] == '\n':
fileName = fileName[:-1]
Georg
.
- 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: Re: remove the last character or the newline character?
- 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
|