TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python String | rstrip() method with Examples

Python String rstrip() method with Examples on capitalize(), center(), count(), encode(), find(), format(), index(), join(), lower(), ljust(), isupper(), istitle(), isspace(), isprintable(), isnumeric(), islower() etc.

<< Back to PYTHON

Python String rstrip() Method

Python rstrip() method removes all the trailing characters from the string. It means it removes all the specified characters from right side of the string. If we don't specify the parameter, It removes all the whitespaces from the string. This method returns a string value.

Signature

rstrip([chars])

Parameters

chars: character to be removed from the string.

Return

It returns string.

Let's see some examples of rstrip() method to understand it's functionality.

Python String rstrip() Method Example 1

A simple example, does not take any parameter. It removes all the trailing whitespaces from the string.

# Python rstrip() method example
# Variable declaration
str = "Java and C# "
# Calling function
str2 = str.rstrip()
# Displaying result
print("Old string: ",str)
print("New String: ",str2)

Output:

Old string:  Java and C# 
New String:  Java and C#

Python String rstrip() Method Example 2

string chars are removed based on the char type parameter. It returns a string after removing the character.

# Python rstrip() method example
# Variable declaration
str = "Java and C#"
# Calling function
str2 = str.rstrip('#')
# Displaying result
print("Old string: ",str)
print("New String: ",str2)

Output:

Old string:  Java and C#
New String:  Java and C

Python String rstrip() Method Example 3

It is pretty easy to know how many characters are removed by getting string length. See the example. String length is displayed along with string value.

# Python rstrip() method example
# Variable declaration
str = "Java and C#"
# Calling function
str2 = str.rstrip('#')
# Displaying result
print("Old string: ",str, len(str))
print("New String: ",str2, len(str2))

Output:

Old string:  Java and C# 11
New String:  Java and C 10

Next TopicPython Strings




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf