Python rstrip: How to Use String rstrip() Function in Python
Python strip() is an inbuilt function that returns the copy of a string with both leading and trailing characters removed based on the string argument passed.
Python String rstrip() is an inbuilt method that returns a copy of the String with trailing characters removed. The rstrip() function returns the copy of the String in which all characters have been stripped from the end of the string and default whitespace characters.
One most important point to note here that these functions do not make inplace changes, and thus, these changes are just temporary.
If you want to make these modifications permanent then, you need to assign it to another variable to store the changed String because it returns the changed String.
Another important thing is by default, and it removes whitespaces if you don’t mention the character.
chars (optional): It is a string specifying the set of characters to be removed.
The rstrip() function returns the copy of the String in which all chars have been stripped from the end of the String (default whitespace characters).
rand_str = ' Harry Potter' print('Source string: \n', rand_str) # Leading whitepsace are removed print('Stripped strings: ') print(rand_str.rstrip()) print(rand_str.rstrip('kl kb')) print(rand_str.rstrip('mj gigi')) website = 'https://facebook.com/' print(website.rstrip('m/.'))
Source string: Harry Potter Stripped strings: Harry Potter Harry Potter Harry Potter https://facebook.co
You can see that in the first instance that leading white space is removed from Harry Potter.
Then in the second and third instances, it won’t remove anything because the characters provided in the argument do not exist in the original String.
In the last instance, we have removed m character from the URL string because it matches with source string website.
rand_str = " DataFrame " print("Pandas" + rand_str.rstrip() + " Example ")
Pandas DataFrame Example
Python rstrip vs. strip
Python strip() method removes both leading and trailing characters and returns the String. While the rstrip() method returns the copy of the String in which all characters have been stripped from the end of the String (default whitespace characters).
We can summarize all the Python string strip functions as follows.
1. strip(): Python strip() function is used to delete all the leading and trailing characters mentioned in its argument. If chars argument is omitted or None, whitespace characters are removed. If the given and not None, chars must be a string; the characters in the String will be stripped from both ends of the String this method is called on.
2. lstrip(): Python lstrip() function is used to remove all the leading characters mentioned in its function parameter. If chars argument is omitted or None, whitespace characters are removed. If given and not None, then the chars must be a string; the characters in the String will be stripped from the starting of the String the lstrip() method is called on.
3. rstrip(): Python rstrip() function is used to remove all the trailing characters mentioned in its function parameter. If chars argument is omitted or None, whitespace characters are removed. If given and not None, then the chars must be a string; the characters in a string will be stripped from the end of a string rstrip() method is called on.
How to remove a trailing newline in Python
If you call string rstrip(chars) method on a string with “\n” as chars to create a new string, then the trailing newline removed. Assign the resultant String to the original String’s variable.
rand_str = "DataFrame\n" print(rand_str) data = "Example" print(data) result = rand_str.rstrip() print(result) data = "Example" print(data)
DataFrame Example DataFrame Example
From the output, you can see that in the first instance, the \n character does execute, and Example is printed on the next line. Still, when we stripped the new line character with rstrip() function, we can see that the new line is removed, and it immediately prints the Example.
Python’s rstrip() method strips all kinds of trailing whitespace by default, not just one newline as Perl does with chomp.
Also, I would say the “pythonic” way to get lines without trailing newline characters is string.splitlines().
rand_str = "Python\nDataFrame\nExample\n" print(rand_str) splitlines = rand_str.splitlines() print(splitlines)
Python DataFrame Example ['Python', 'DataFrame', 'Example']
The canonical way to strip end-of-line (EOL) characters is to use the string rstrip() method removing any trailing \r or \n. You can use this for Mac, Windows, and Unix EOL characters. Using ‘\r\n’ as the parameter to rstrip means that it will strip out any trailing combination of ‘\r’ or ‘\n’. That’s why it works in all these OS cases.
Finally, Python rstrip() example is over.