By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

remove the last character or the newline character?

P: n/a
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

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

Thank very much!
-Daniel

Sep 28 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.
Sep 28 '06 #2

P: n/a
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
Sep 28 '06 #3

P: n/a
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
You're close...

fileName = fileName[0:-1]

which is the same as

fileName = fileName[:-1]

which will lop off the last character. Much nicer than most
other languages I've used where you have to use the len() trick
you're using. Also, it's a common python idiom you'll see
frequently.
Question two:
Does python provide any function that can remove the newline character
from a string if it exists?
In a discussion on this very matter a while back, I think the
final verdict was something like

fileName = fileName.rstrip('\n')

which will strip off *all* the trailing newlines. In most cases
(such as "for line in file('foo.txt'):" code), there's only one
to be stripped, so this works fine.

If you're ornary and want *only* the last '\n' lopped off, you'd
have to test for it:

if fileName[-1] == '\n': fileName = fileName[:-1]

There were caveats regarding "\n" vs. "\r\n" line endings, but if
the file comes in in ascii mode (rather than binary mode), Python
more or less smart enough to do the translation for you, leaving
the above code to work in the majority of cases.

-tkc


Sep 28 '06 #4

P: n/a
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 'o****@xiludom.gro'.split('@')])"
Sep 28 '06 #5

P: n/a
"Daniel Mark" <da***********@gmail.comwrote:

In [2]: fileName = fileName[0:len(fileName)-1)] # remove the '\n'
To chop the last character regardless of what it is:

fileName = fileName[:-1]

You don't need the 0 since thats the default, and you can use a negative
index instead of subtracting from len(x).
Question two:
Does python provide any function that can remove the newline character
from a string if it exists?
To remove all trailing whitespace:

fileName = fileName.rstrip()

to just remove a newline:

fileName = fileName.rstrip('\n')
Sep 28 '06 #6

P: n/a
>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.
Just a caveat with the non-qualified strip/rstrip/lstrip...it
will remove *all* whitespace, so if, for some reason, it's
significant, and you only want to remove newlines, you have to
specify it:
>>s = ' some text \t\n'
s.strip()
'some text'
>>s.rstrip()
' some text'
>>s.rstrip('\n')
' some text \t'

As the OP was talking about file-names, the use of
initial/terminal spaces is allowable (albeit imprudent),
depending on your platform, so one may or may not want to strip
them off.

Fortunately, as in many other areas, Python offers the
flexibility in an easy-to-use way, and comes with sensible defaults.

-tkc


Sep 28 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.