Introduction to Python Endswith
The endswith() function in Python is a conditional statement that the user can use to check if a given value or string ends with a specified character or substring. When the condition is satisfied, it returns True, and when the condition is not met, it returns False. Additionally, there are optional start and end parameters, which are integers that define the range within the string where the condition should be applied. By using the endswith() function, users can easily determine whether a given value ends with a particular character or substring.
Syntax
The basic syntax of the Endswith statement has a string or the text followed by Endswith statement and round bracket, and inside the bracket, we can specify the value or string we want to check.
string/text.endswith(value, start, end)
Explanation: Start and End are optional values we can specify to narrow down our condition with specific start and end values.
Example Syntax:
Code:
statement = "Hello, Have a great day."
x = statement.endswith("day.")
print(x)
Output:
How does Endswith Work?
In the Endswith statement, the suffix we denote as the condition is very important because the Endswith statement reads the entire statement and only when the user-denoted suffix exactly matches the condition it returns True otherwise, it will fail to check the condition and returns False. We should not forget the special characters, comma, and full stops when giving the condition because the Endswith statement also reads such characters in the suffix. If our original statement or string has such characters and we fail to give those in conditions, then Endswith condition gives us the boolean output: False.
Examples to Implement Python Endswith
Below are the examples mentioned :
Example #1
Code:
statement = "Hello, Have a great day."
a = statement.endswith("day.")
b=statement.endswith("day")
c=statement.endswith("great day")
d=statement.endswith("Hello, Have a great day.")
print(a)
print(b)
print(c)
print(d)
Output:
Explanation: In the above example, we can see how the Python Endswith works; we have declared a statement called “Hello, Have a great day.” and checked the Endswith statement with four types of conditions. In the condition, we have exactly denoted the last word in the statement “day” along with the full stop we used in our statement, so the Endswith statement as expected returns us True.
In the second condition b, we have denoted the word “day” and did not type in the full stop in the condition, so Endswith statement checks and returns as False. In the third condition (c), an additional word, “great,” followed by “day” is included. Both of these words are at the end of the statement. However, if the full stop (.) is not included in the condition, the endswith() statement will return False. In the last condition, we have typed in the entire statement as it is, along with the full stop, and hence the Endswith statement identifies it and returns us True.
Example #2
Let us discuss more the Endswith function with start and end parameters which are the integers that represent the position of the string in our Endswith statement returns False.
Code:
statement = "This is a sample string"
a = statement.endswith("g",22)
b=statement.endswith("g",23)
c=statement.endswith("g",14)
print(a)
print(b)
print(c)
Output:
Explanation: In this example, we have denoted the start parameter and our string condition “g”. In our statement “This is a sample string” the “g” we have denoted as the condition is at the last position, which is the 22nd position of our statement.
In the next condition b, we have passed an integer of 23 which is more than the starting point of the letter “g” of the statement, and the total characters starting from 0 is only 22 in our whole statement, so there is no character in the position 23 hence Endswith function returns False. In the last condition, c, we have denoted a position 14, below the starting point of the letter “g” and any position that is less or below our conditional value, the Endswith function returns us with True.
Example #3
Using the start and end parameters which will give us a better understanding of the Endswith function.
Code:
statement = "This is a sample string"
a = statement.endswith("s",0,7)
b=statement.endswith("s",0,8)
c=statement.endswith("a",5,9)
print(a)
print(b)
print(c)
Output:
Explanation: In the above example, we have used the same statement and passed different conditions with the start and end parameters. In the first condition, we have passed the string s and integers of 0 and 7 as start and endpoints. The string “s” is in position 6 in the statement, and when we give the starting point as 0 and endpoint as which is the start and endpoint of the string “s,” our condition is satisfied, and hence we get the output as True. In the next condition, b, we have passed the start point as 0 and the endpoint as 8, which is outside the position of the string “s” hence Endswith function gives us the output as False.
In the 3rd condition c, we have the string “a,” which is at the 9th position in the statement, and the starting point can be anywhere from 0 to 8 where the string “a” is present, and if we pass the starting point greater than 8 which violates the condition since the starting point of “a” is at 8 Endswith functions returns the output as False.
Conclusion
We have discussed in detail the Endswith function in Python, the definition, and the way it works with simple examples for better understanding. We have also discussed with clear examples of the start and end parameters used in the Endswith function so that we can narrow down or specify the condition we wanted to check using the Endswith function. The Endswith function is very helpful in Python programming when we want to check for the position of a string by passing our conditions through the Endswith function.
Recommended Articles
This is a guide to Python Endswith. Here we discuss an introduction to Python Endswith, syntax, how it works, and programming examples. You can also go through our other related articles to learn more –