{"id":24531,"date":"2021-10-21T10:04:41","date_gmt":"2021-10-21T04:34:41","guid":{"rendered":"https:\/\/python-programs.com\/?p=24531"},"modified":"2021-11-05T19:05:30","modified_gmt":"2021-11-05T13:35:30","slug":"python-string-strip-method-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-string-strip-method-examples\/","title":{"rendered":"Python String strip() Method Examples"},"content":{"rendered":"
In the previous article, we have discussed Python String rstrip() Method Examples<\/a> The strip() method removes any leading (at the start) and trailing (at the end) characters (space is the default leading character to remove)<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n characters:<\/strong> This is optional. A set of characters that should be removed as leading\/trailing characters.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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<\/a> Tutorial & meet such challenges with ease.<\/p>\n 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 …<\/p>\n
\nstrip() Method in Python:<\/strong><\/p>\nstring.strip(characters)<\/pre>\n
Given first string = \"!!!!!!!_python-programs_!!!!!\"\r\nGiven characters = \"!\"<\/pre>\n
The above given first string is : !!!!!!!_python-programs_!!!!!\r\nThe given first string after applying strip() function: _python-programs_<\/pre>\n
Given second string = \"^^^^***btechgeeks^^^***\"\r\nGiven characters = \"^*\"<\/pre>\n
The above given second string is : ^^^^***btechgeeks^^^***\r\nThe given second string after applying strip() function: btechgeeks<\/pre>\n
String strip() Method Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the first string as static input and store it in a variable.\r\ngvn_fststr = \"!!!!!!!_python-programs_!!!!!\"\r\n# Give the characters as static input and store it in another variable.\r\ngvn_chrctr = \"!\"\r\n# Apply strip() method to the given string for the given character to remove\r\n# any leading (at the start) and trailing (at the end) characters\r\n# (space is the default leading character to remove)\r\n# Store it in another variable.\r\nrslt_str1 = gvn_fststr.strip(gvn_chrctr)\r\n# Print the above given string\r\nprint(\"The above given first string is :\", gvn_fststr)\r\n# Print the above given string after applying strip() function.\r\nprint(\"The given first string after applying strip() function:\", rslt_str1)\r\n# Similarly do the same for other string without giving the characters\r\n# and print the result string.\r\ngvn_scndstr = \" good morning \"\r\nrslt_str2 = gvn_scndstr.strip()\r\nprint(\"The above given second string is :\", gvn_scndstr)\r\nprint(\"The given second string after applying strip() function:\", rslt_str2)\r\n<\/pre>\n
The above given first string is : !!!!!!!_python-programs_!!!!!\r\nThe given first string after applying strip() function: _python-programs_\r\nThe above given second string is : good morning \r\nThe given second string after applying strip() function: good morning<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the first string as user input using the input() function and store it in a variable.\r\ngvn_fststr = input(\"Enter some random string = \")\r\n# Give the characters as user input using the input() function and store it in another variable.\r\ngvn_chrctr = input(\"Enter some random characters = \")\r\n# Apply strip() method to the given string for the given character to remove\r\n# any leading (at the start) and trailing (at the end) characters\r\n# (space is the default leading character to remove)\r\n# Store it in another variable.\r\nrslt_str1 = gvn_fststr.strip(gvn_chrctr)\r\n# Print the above given string\r\nprint(\"The above given first string is :\", gvn_fststr)\r\n# Print the above given string after applying strip() function.\r\nprint(\"The given first string after applying strip() function:\", rslt_str1)\r\n# Similarly do the same for other string and print the result string.\r\ngvn_scndstr = input(\"Enter some random string = \")\r\ngvn_chrctr2 = input(\"Enter some random characters = \")\r\nrslt_str2 = gvn_scndstr.strip(gvn_chrctr2)\r\nprint(\"The above given second string is :\", gvn_scndstr)\r\nprint(\"The given second string after applying strip() function:\", rslt_str2)\r\n<\/pre>\n
Enter some random string = ^^^^***btechgeeks^^^***\r\nEnter some random characters = ^*\r\nThe above given first string is : ^^^^***btechgeeks^^^***\r\nThe given first string after applying strip() function: btechgeeks\r\nEnter some random string = hello all ####\r\nEnter some random characters = #\r\nThe above given second string is : hello all ####\r\nThe given second string after applying strip() function: hello all<\/pre>\n
\n