python: rstrip one exact string, respecting order
Question: Is it possible to use the python command `rstrip` so that it does only remove
one exact string and does not take all letters separately?

I was confused when this happened:

    
    
    >>>"Boat.txt".rstrip(".txt")
    >>>'Boa'
    

What I expected was:

    
    
    >>>"Boat.txt".rstrip(".txt")
    >>>'Boat'
    

Can I somehow use rstrip and respect the order, so that I get the second
outcome?


Answer: You're using wrong method. Use
[`str.replace`](http://docs.python.org/2/library/stdtypes#std.replace)
instead:

    
    
    >>> "Boat.txt".replace(".txt", "")
    'Boat'
    

**NOTE** : `str.replace` will replace anywhere in the string.

    
    
    >>> "Boat.txt.txt".replace(".txt", "")
    'Boat'
    

To remove the last trailing `.txt` only, you can use [_regular
expression_](http://docs.python.org/2/library/re.html):

    
    
    >>> import re
    >>> re.sub(r"\.txt$", "", "Boat.txt.txt")
    'Boat.txt'
    

If you want filename without extension,
[`os.path.splitext`](http://docs.python.org/2/library/os.path.html#os.path.splitext)
is more appropriate:

    
    
    >>> os.path.splitext("Boat.txt")
    ('Boat', '.txt')
    

