remove the last character or the newline character?

D

Daniel Mark

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
 
J

John Salerno

Daniel said:
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?

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

Georg Brandl

Daniel said:
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
 
B

Bruno Desthuilliers

Daniel said:
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
 
D

Duncan Booth

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')
 
T

Tim Chase

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
 
T

Tim Chase

Does python provide any function that can remove the newline character
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:
' 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top