Python string endswith() method

Python String endswith() Method

Description

The endswith() method returns True if a string ends with the specified substring, and False otherwise. It optionally matches using given start and end indices.

Syntax

var.endswith(sub[, start[, end]])

Parameters

  • sub − This can be a string or a tuple of suffixes to search for.
  • start − The starting position of the slice.
  • end − The ending position of the slice.

Return Value

Returns TRUE if the string ends with the specified suffix; otherwise, returns FALSE.

Example

var = "Explicit is better than implicit."

var1 = var.endswith('t.')
print ("Original string:", var)
print ("Ends with t.:", var1)

var2 = var.endswith('be', 0, 14)
print ("Left substring up to index 14 ends with be:", var2)

When running this program, the following output is produced −

Original string: Explicit is better than implicit.
Ends with t.: True
Left substring up to index 14 ends with be: True

Leave a Reply

Your email address will not be published. Required fields are marked *