remove the last character or the newline character?

Discussion in 'Python' started by Daniel Mark, Sep 28, 2006.

  1. Daniel Mark

    Daniel Mark Guest

    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
     
    Daniel Mark, Sep 28, 2006
    #1
    1. Advertising

  2. Daniel Mark

    John Salerno Guest

    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.
     
    John Salerno, Sep 28, 2006
    #2
    1. Advertising

  3. Daniel Mark

    Georg Brandl Guest

    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
     
    Georg Brandl, Sep 28, 2006
    #3
  4. 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 ''.split('@')])"
     
    Bruno Desthuilliers, Sep 28, 2006
    #4
  5. Daniel Mark

    Duncan Booth Guest

    "Daniel Mark" <> wrote:


    > 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')
     
    Duncan Booth, Sep 28, 2006
    #5
  6. Daniel Mark

    Tim Chase Guest

    > 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
     
    Tim Chase, Sep 28, 2006
    #6
  7. Daniel Mark

    Tim Chase Guest

    >> 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
     
    Tim Chase, Sep 28, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. peelman
    Replies:
    1
    Views:
    14,320
    Karl Seguin
    Oct 18, 2005
  2. gk
    Replies:
    9
    Views:
    7,170
  3. Charles Erwin
    Replies:
    4
    Views:
    594
    Joe Wright
    Oct 6, 2003
  4. Johny
    Replies:
    8
    Views:
    414
  5. Johannes Schaub (litb)

    Output of a program that doesn't print a newline at last

    Johannes Schaub (litb), Mar 10, 2010, in forum: C Programming
    Replies:
    2
    Views:
    314
    Phred Phungus
    Mar 10, 2010
Loading...

Share This Page