{"id":24478,"date":"2021-10-17T18:48:42","date_gmt":"2021-10-17T13:18:42","guid":{"rendered":"https:\/\/python-programs.com\/?p=24478"},"modified":"2021-11-05T19:25:29","modified_gmt":"2021-11-05T13:55:29","slug":"python-string-lstrip-method-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-string-lstrip-method-examples\/","title":{"rendered":"Python String lstrip() Method Examples"},"content":{"rendered":"
In the previous article, we have discussed Python Program for ljust() Function<\/a> Any leading characters are removed using the lstrip() method (space is the default leading character to remove)<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n characters:<\/strong> This parameter is optional, and omitting it causes the lstrip function to treat white spaces as the default parameter. In Python, specify the Characters to strip from the left-hand side of a string to change the default white space.<\/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 Program for ljust() Function lstrip() Function in Python: Any leading characters are removed using the lstrip() method (space is the default leading character to remove) Syntax: string.lstrip(characters) Parameters characters: This parameter is optional, and omitting it causes the lstrip function to treat white spaces as the default …<\/p>\n
\nlstrip() Function in Python:<\/strong><\/p>\nstring.lstrip(characters)<\/pre>\n
Given first String = \"####python###\"\r\nGiven character = '#'<\/pre>\n
The above given first string is : ####python###\r\nThe given first string after applying lstrip() function: python###<\/pre>\n
Given second String = \"@@@@@###_GOODMORNING_22222\"\r\nGiven characters = \"@,#\"<\/pre>\n
The above given second string is : @@@@@###_GOODMORNING_22222\r\nThe given second string after applying lstrip() function: _GOODMORNING_22222<\/pre>\n
Program for lstrip() Function 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###\"\r\n# Give the character as static input and store it in another variable.\r\ngvn_chactr = '#'\r\n# Apply lstrip() method to the given string for the given character in which\r\n# any leading characters are removed using the lstrip() method\r\n# (space is the default leading character to remove).\r\n# Store it in another variable.\r\nrslt_str1 = gvn_fststr.lstrip(gvn_chactr)\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 lstrip() function.\r\nprint(\"The given first string after applying lstrip() function:\", rslt_str1)\r\n# Similarly do the same for other string and print the result string.\r\ngvn_scndstr = \"@@@@@###_GOODMORNING_22222\"\r\nrslt_str2 = gvn_scndstr.lstrip()\r\nprint(\"The above given second string is :\", gvn_scndstr)\r\nprint(\"The given second string after applying lstrip() function:\", rslt_str2)\r\n<\/pre>\n
The above given first string is : ####python###\r\nThe given first string after applying lstrip() function: python###\r\nThe above given second string is : @@@@@###_GOODMORNING_22222\r\nThe given second string after applying lstrip() function: @@@@@###_GOODMORNING_22222<\/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 character as user input using the input() function and store it in another variable.\r\ngvn_chactr = input(\"Enter some random character = \")\r\n# Apply lstrip() method to the given string for the given character in which\r\n# any leading characters are removed using the lstrip() method\r\n# (space is the default leading character to remove).\r\n# Store it in another variable.\r\nrslt_str1 = gvn_fststr.lstrip(gvn_chactr)\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 lstrip() function.\r\nprint(\"The given first string after applying lstrip() 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_chactr2 = input(\"Enter some random character = \")\r\nrslt_str2 = gvn_scndstr.lstrip(gvn_chactr2)\r\nprint(\"The above given second string is :\", gvn_scndstr)\r\nprint(\"The given second string after applying lstrip() function:\", rslt_str2)\r\n<\/pre>\n
Enter some random string = hello all\r\nEnter some random character = ^\r\nThe above given first string is : hello all\r\nThe given first string after applying lstrip() function: hello all\r\nEnter some random string = 555555python_platform_4545\r\nEnter some random character = 5\r\nThe above given second string is : 555555python_platform_4545\r\nThe given second string after applying lstrip() function: python_platform_4545<\/pre>\n
\n