Python String endswith() Method



The python string endswith() method checks if the input string ends with the specified suffix. This function returns true if the string ends with the specified suffix, otherwise returns false.

This function has one mandatory parameter and two optional parameters. The mandatory parameter is a string that needs to be checked and optional parameters are the start and end indexes. By default, the start index is 0 and the end index is length -1.

In the following section, we will be learning more details about the python string endswith() method.

Syntax

The following is the syntax for the python string endswith() method.

str.endswith(suffix[, start[, end]])

Parameters

The parameters of the python string endswith() method are as follows.

  • suffix − This parameter specifies a string or a tuple of suffixes to look for.

  • start − This parameter specifies the starting index to search.

  • end − This parameter specifies the ending index where the search ends.

Return Value

The python string endswith() method returns true if the string ends with the specified suffix and otherwise, it returns false.

Example

The python string endswith() method applied on a string with a suffix as its parameter will return a boolean value true if the string ends with that suffix. Else, it returns false.

The following is an example in which a string "Hello!Welcome to Tutorialspoint." is created and the suffix 'oint' is also specified. Then, endswith() function is invoked on the string with only the suffix as its argument and the result is printed as output using the print() function.

str = "Hello!Welcome to Tutorialspoint.";
suffix = "oint.";
result=str.endswith(suffix)
print("The input string ends with the given suffix:", result)

On executing the above program, the following output is generated -

The input string ends with the given suffix: True

Example

The python string endswith() method applied on a string with a suffix, start index as its parameters will return a boolean value true if the string ends with that suffix and starts from the specified start index. Else, it returns false.

The following is an example in which a string "Hello!Welcome to Tutorialspoint." is created and the suffix 'oint' is also specified. Then, endswith() function is invoked on the string with the suffix and the start index '28' passed as its arguments and the result is printed as output using the print() function.

str = "Hello!Welcome to Tutorialspoint.";
suffix = "oint.";
result=str.endswith(suffix, 28)
print("The input string ends with the given suffix:",result)

The following is the output obtained by executing the above program -

The input string ends with the given suffix: False

Example

The python string endswith() method applied on a string with a suffix, start index and end index as its parameters will return a boolean value true if the string ends with that suffix in the given range. Else, it returns false.

The following is an example in which a string "Hello!Welcome to Tutorialspoint." is created and the suffix 'oint.' is also specified. Then, endswith() function is invoked on the string with the suffix, the start as '27' and the end index as '32' passed as its arguments and the result is printed as output using the print() function.

str = "Hello!Welcome to Tutorialspoint.";
suffix = "oint.";
result=str.endswith(suffix, 27, 32)
print("The input string ends with the given suffix:",result)

The above program, on executing, displays the following output -

The input string ends with the given suffix: True

Example

The python string endswith() method's first parameter must be in the form of a string which specifies the suffix that is to be checked in the input string. If the string is not specified in its parameter, a type error is occured.

The following is an example in which a string "Hello!Welcome to Tutorialspoint." is created and then, endswith() function is invoked on the string and the start index as '27' and the end index as '32' is passed as its arguments and the result is printed as output using the print() function.

str = "Hello!Welcome to Tutorialspoint.";
result=str.endswith(27, 32)
print("The input string ends with the given suffix:",result)

The following is the output displayed on executing the above program -

Traceback (most recent call last):
  File "main.py", line 2, in 
    result=str.endswith(27, 32)
TypeError: endswith first arg must be str or a tuple of str, not int

Example

The python string endswith method takes atleast one argument. If no arguments are specified, then a type error is occured.

The following is an example in which a string "Hello!Welcome to Tutorialspoint." is created and then, endswith() function is invoked on the string with no arguments passed and the result is printed as output using the print() function.

str = "Hello!Welcome to Tutorialspoint.";
result=str.endswith()
print("The input string ends with the given suffix:",result)

The output of the above program is displayed as follows -

Traceback (most recent call last):
  File "main.py", line 2, in 
    result=str.endswith()
TypeError: endswith() takes at least 1 argument (0 given)
python_strings.htm
Advertisements