{"id":24767,"date":"2021-11-02T09:46:14","date_gmt":"2021-11-02T04:16:14","guid":{"rendered":"https:\/\/python-programs.com\/?p=24767"},"modified":"2021-11-05T19:16:13","modified_gmt":"2021-11-05T13:46:13","slug":"python-string-endswith-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-string-endswith-method-with-examples\/","title":{"rendered":"Python String endswith() Method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python String count() Method with Examples<\/a> If the string ends with the specified value, the endswith() method returns true; otherwise, it returns False.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n value:<\/strong> This is Required. The value used to determine whether the string ends with<\/p>\n start:<\/strong> This is optional. It is an integer. The starting point for the search. The default value is 0.<\/p>\n end:<\/strong> This is optional. It is an integer. The ending point for the search. The default value is the end of the string.<\/p>\n Return Value:<\/strong><\/p>\n The endswith() method gives a boolean value.<\/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 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 Go through our tutorial and learn about various Python String Method Examples<\/a> and learn how to apply the knowledge while dealing with strings.<\/p>\n In the previous article, we have discussed Python String count() Method with Examples String endswith() Method in Python: If the string ends with the specified value, the endswith() method returns true; otherwise, it returns False. Syntax: string.endswith(value, start, end) Parameters value: This is Required. The value used to determine whether the string ends with start: …<\/p>\n
\nString endswith() Method in Python:<\/strong><\/p>\nstring.endswith(value, start, end)<\/pre>\n
\n
Given string = \"hello this is btechgeeks.good morning btechgeeks\"\r\nGiven value = \"hello\"<\/pre>\n
Checking if the given string endswith{ hello } or not = False<\/pre>\n
Given string = \"welcome to python programs\"\r\nGiven value = \"e\"\r\nstart position = 0\r\nend position = 7<\/pre>\n
Checking if the given string endswith{ e } or not = True<\/pre>\n
String endswith() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
1)Without giving start and end positions<\/h5>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = \"hello this is btechgeeks.good morning btechgeeks\"\r\n# Give the value as static input and store it in another variable.\r\ngvn_valu = \"geeks\"\r\n# Pass the given value as an argument to the endswith() function for the\r\n# given string to check if the given string ends with the given value or not.\r\n# Store it in another variable.\r\nrslt = gvn_str.endswith(gvn_valu)\r\n# Print the result after checking If a given string ends with the given\r\n# value or not.\r\nprint(\r\n \"Checking if the given string endswith{\", gvn_valu, \"} or not = \", rslt)\r\n<\/pre>\n
Checking if the given string endswith{ geeks } or not = True<\/pre>\n
2)With giving start and end positions<\/h5>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = \"welcome to python programs\"\r\n# Give the value as static input and store it in another variable.\r\ngvn_valu = \"e\"\r\n# Give the start position as static input and store it in another variable.\r\ngvn_strtpositn = 0\r\n# Give the end position as static input and store it in another variable.\r\ngvn_endpositn = 7\r\n# Pass the given value, start and end positions as arguments to the endswith()\r\n# function for the given string to check if the given string ends with the\r\n# given value or not in the given start and end range.\r\n# Store it in another variable.\r\nrslt = gvn_str.endswith(gvn_valu, gvn_strtpositn, gvn_endpositn)\r\n# Print the result after checking If a given string ends with the given\r\n# value or not.\r\nprint(\r\n \"Checking if the given string endswith{\", gvn_valu, \"} or not = \", rslt)\r\n<\/pre>\n
Checking if the given string endswith{ e } or not = True<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
1)Without giving start and end positions<\/h5>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Enter some Random String = \")\r\n# Give the value as user input using the input() function and store it in another variable.\r\ngvn_valu = input(\"Enter some Random String(value) = \")\r\n# Pass the given value as an argument to the endswith() function for the\r\n# given string to check if the given string ends with the given value or not.\r\n# Store it in another variable.\r\nrslt = gvn_str.endswith(gvn_valu)\r\n# Print the result after checking If a given string ends with the given\r\n# value or not.\r\nprint(\r\n \"Checking if the given string endswith{\", gvn_valu, \"} or not = \", rslt)\r\n<\/pre>\n
Enter some Random String = hello this is btechgeeks\r\nEnter some Random String(value) = hello\r\nChecking if the given string endswith{ hello } or not = False<\/pre>\n
2)With giving start and end positions<\/h5>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Enter some Random String = \")\r\n# Give the value as user input using the input() function and store it in another variable.\r\ngvn_valu = input(\"Enter some Random String(value) = \")\r\n# Give the start position as user input using the int(input()) function and \r\n# store it in another variable.\r\ngvn_strtpositn = int(input(\"Enter some random number = \"))\r\n# Give the end position as user input using the int(input()) function and \r\n# store it in another variable.\r\ngvn_endpositn = int(input(\"Enter some random number = \"))\r\n# Pass the given value, start and end positions as arguments to the endswith()\r\n# function for the given string to check if the given string ends with the\r\n# given value or not in the given start and end range.\r\n# Store it in another variable.\r\nrslt = gvn_str.endswith(gvn_valu, gvn_strtpositn, gvn_endpositn)\r\n# Print the result after checking If a given string ends with the given\r\n# value or not.\r\nprint(\r\n \"Checking if the given string endswith{\", gvn_valu, \"} or not = \", rslt)\r\n\r\n\r\n\r\n<\/pre>\n
Enter some Random String = welcome to python programs\r\nEnter some Random String(value) = p\r\nEnter some random number = 2\r\nEnter some random number = 6\r\nChecking if the given string endswith{ p } or not = False<\/pre>\n
\n