What is rstrip() in python?

Will Acheson willyach07 at gmail.com
Tue Nov 11 20:53:28 CET 2014


On Sunday, November 9, 2014 6:12:24 AM UTC-5, satish... at gmail.com wrote:
> What is rstrip() in python?
> 
> What does it do in the following piece of code?
> 
> import sqlite3
> conn = sqlite3.connect('dbase1')
> curs = conn.cursor()
> 
> file = open('data.txt')
> rows = [line.rstrip().split(',') for line in file]

rstrip() removes whitespace, newline characters, tab characters, and carrige return characters (\n \t \r respectively) on the tail of a string.  

Or it can be used with an input parameter to remove all instances of that parameter from the tail of a string.

ex:
 stringy = "i am helpful  \t\t\t\t\n\n      "
 stringy = stringy.rstrip()
 print stringy

stdout: "i am helpful"

or:
 stringy = "my favorite number is 8000000000000000000000"
 stringy = stringy.rstrip('0')
 print stringy

stdout: "my favorite number is 8"



pretty simple method, helpful for parsing out formatting characters from scraped content from webpages.
https://docs.python.org/2/library/stdtypes.html?highlight=rstrip#str.rstrip



More information about the Python-list mailing list