×

Python String endswith() method

Python String endswith() method

The string.endswith() method in Python returns a Boolean value True if the string ends with the specified suffix, otherwise it returns False.

Syntax

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

Parameter

suffix – This parameter represents a string or tuple of suffixes to be checked.

start (optional) – This parameter signifies the beginning position where suffix is to be checked within the string.

end (optional) - This parameter signifies the ending position where suffix is to be checked within the string.

Return

The method returns a Boolean value True if the strings ends with the specified suffix else it returns False if the string doesn't end with the specified suffix.

Example 1

 # Python program explaining
 # the endswith() method
 # passing the string value
 str_val = "This is my Python endswith program. This is a simple method."
 # checking whether the string ends with the specified suffix,
 result1 = str_val.endswith('simple method.')
 # returns False
 print(result1)
 result2 = str_val.endswith('endswith program.')
 # returns True
 print(result2)
 result3 = str_val.endswith('This is my Python endswith program. This is a simple method.')
 # returns True
 print(result3) 

Output

 True
 False
 True 

Example 2

 # Python program explaining
 # the endswith() method
 strVal = "tutorials and examples."
 # start parameter: 10 
 result1 = strVal.endswith('tutorials.', 10) 
 print(result1) 
 # Both start and end is provided 
 # start: 14, end: 22 
 # Returns true 
 result2 = strVal.endswith('examples', 14, 22) 
 print (result2)
 # returns True 
 result3 = strVal.endswith('tutorials', 0, 9) 
 print (result3) 

Output

 False
 True
 True 

Related Topics

PyShark in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Working with JSON in Python

Python is an Object-Oriented high-level language. Python is designed to be highly beginner-friendly. Python has an English-like syntax, which is very easy to read. In this article, we are going...

4 minutes read.

Python program to find factorial of a given number

Python program to find factorial of a given number Factorial is a non-negative integer. It is the product of all positive integers from 1 to the number we are going to...

3 minutes read.

Iterate through the list in Python

One of the six basic data types of the Python computer language is the list. You must be familiar with the methods and functions that deal with lists in order...

7 minutes read.

Python BS4 Code

Python BS4 Code The BS4 stands for BeautifulSoup version 4.x. The BeautifulSoup is a Python library which is used for pulling out data of the HTML & XML files using the...

14 minutes read.

Scraping data in python

Data scraping is a technique in which one program extracts a set of data from the output of another program. Web scraping is the most common application of this technique....

6 minutes read.

Comments in the Python Programming Language

In this tutorial, we will learn how to comment in multiple lines and paragraphs in the python programming language. Commenting a Paragraph in Python Most people think the importance of comments is...

2 minutes read.

Filter List in Python

Python: Python is one of the most used programming languages, as it is used widely in software and data analysis, web development, etc. It is said to be a user-friendly programming...

3 minutes read.

Python program to print calendar

Python program to print the calendar This article will explain how to print the calendar of month and year using Python's calendar module. It's a straightforward thing in Python by importing...

1 minute read.

Python Percentage Sign

In Python, the percentage sign significantly completes two things. They are: It goes about as a Modulo administrator. It helps in string organizing. Allow us to see every one of them plainly. Modulo operator: Like...

2 minutes read.

Paramiko Python Example

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Accuracy_score Function in Sklearn

A crucial stage in data science is measuring our model's performance using the appropriate metric. In this article, we will examine two methods for calculating the accuracy of your predictions:...

13 minutes read.

How to set font for Text in Python

As a tuple with the font family as the first component, a size in point as the second, and possibly a string with one or more of the style modifiers...

5 minutes read.

Nested List in Python

The list is an inbuilt sequential data type in Python. It is a very useful and frequently used data type. A list can store any number of data items of...

4 minutes read.

Python bytearray()

Python bytearray() Class The bytearray() class is used to return a bytearray object which is an array of the specified bytes. It gives a mutable sequence of integers in the range 0...

1 minute read.

Python Program for Linear Search

Introduction We employ specific algorithms to efficiently carry out our responsibilities, such as searching for an element in a given data structure. These algorithms fall under the category of searching algorithms....

4 minutes read.

Io stringio Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

7 minutes read.

Python oct() function

Python oct() function The oct() function in Python converts an integer number to an octal string prefixed with “0o”. Syntax oct(x) Parameter x: This parameter represents an Integer Number Return This function returns an octal string. Example 1 #...

1 minute read.

Python String zfill() method

Python String zfill() method The string.zfill() method in Python returns a copy of the string while adding the zeros (0) at the beginning of the string, until it reaches the specified...

1 minute read.

Convert Float to Int in Python using Pandas

Introduction To play with huge amounts of data, in python we require a tool. The tool which is available in Python is pandas. A panda is an open-source library. It is...

4 minutes read.