In the previous article, we have discussed Python String rstrip() Method Examples
strip() Method in Python:
The strip() method removes any leading (at the start) and trailing (at the end) characters (space is the default leading character to remove)
Syntax:
string.strip(characters)
Parameters
characters: This is optional. A set of characters that should be removed as leading/trailing characters.
Examples:
Example1:
Input:
Given first string = "!!!!!!!_python-programs_!!!!!" Given characters = "!"
Output:
The above given first string is : !!!!!!!_python-programs_!!!!! The given first string after applying strip() function: _python-programs_
Example2:
Input:
Given second string = "^^^^***btechgeeks^^^***" Given characters = "^*"
Output:
The above given second string is : ^^^^***btechgeeks^^^*** The given second string after applying strip() function: btechgeeks
String strip() Method Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the first string as static input and store it in a variable.
- Give the character as static input and store it in another variable.
- Apply strip() method to the given string for the given character to remove any leading (at the start) and trailing (at the end) characters (space is the default leading character to remove)
- Store it in another variable.
- Print the above-given string
- Print the above-given string after applying the strip() function.
- Similarly, do the same for other the string without giving the character and print the result string.
- The Exit of Program.
Below is the implementation:
# Give the first string as static input and store it in a variable. gvn_fststr = "!!!!!!!_python-programs_!!!!!" # Give the characters as static input and store it in another variable. gvn_chrctr = "!" # Apply strip() method to the given string for the given character to remove # any leading (at the start) and trailing (at the end) characters # (space is the default leading character to remove) # Store it in another variable. rslt_str1 = gvn_fststr.strip(gvn_chrctr) # Print the above given string print("The above given first string is :", gvn_fststr) # Print the above given string after applying strip() function. print("The given first string after applying strip() function:", rslt_str1) # Similarly do the same for other string without giving the characters # and print the result string. gvn_scndstr = " good morning " rslt_str2 = gvn_scndstr.strip() print("The above given second string is :", gvn_scndstr) print("The given second string after applying strip() function:", rslt_str2)
Output:
The above given first string is : !!!!!!!_python-programs_!!!!! The given first string after applying strip() function: _python-programs_ The above given second string is : good morning The given second string after applying strip() function: good morning
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the first string as user input using the input() function and store it in a variable.
- Give the character as user input using the input() function and store it in another variable.
- Apply strip() method to the given string for the given character to remove any leading (at the start) and trailing (at the end) characters (space is the default leading character to remove)
- Store it in another variable.
- Print the above-given string
- Print the above-given string after applying the strip() function.
- Similarly, do the same for other the string and print the result string.
- The Exit of Program.
Below is the implementation:
# Give the first string as user input using the input() function and store it in a variable. gvn_fststr = input("Enter some random string = ") # Give the characters as user input using the input() function and store it in another variable. gvn_chrctr = input("Enter some random characters = ") # Apply strip() method to the given string for the given character to remove # any leading (at the start) and trailing (at the end) characters # (space is the default leading character to remove) # Store it in another variable. rslt_str1 = gvn_fststr.strip(gvn_chrctr) # Print the above given string print("The above given first string is :", gvn_fststr) # Print the above given string after applying strip() function. print("The given first string after applying strip() function:", rslt_str1) # Similarly do the same for other string and print the result string. gvn_scndstr = input("Enter some random string = ") gvn_chrctr2 = input("Enter some random characters = ") rslt_str2 = gvn_scndstr.strip(gvn_chrctr2) print("The above given second string is :", gvn_scndstr) print("The given second string after applying strip() function:", rslt_str2)
Output:
Enter some random string = ^^^^***btechgeeks^^^*** Enter some random characters = ^* The above given first string is : ^^^^***btechgeeks^^^*** The given first string after applying strip() function: btechgeeks Enter some random string = hello all #### Enter some random characters = # The above given second string is : hello all #### The given second string after applying strip() function: hello all
Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.