Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Python endswith

by suresh

Python endswith method is one of the Python String Method, which returns Boolean TRUE if the string ends with the specified substring. Otherwise, Python endswith function returns False. The Python endswith Function is very useful when we want to check the ending term of User inputs.

Python endswith Syntax

The basic syntax of the Python endswith function is

String_Value.endswith(Substring, Starting_Position, Ending_Position)
  • String_Value: A valid String literal.
  • Substring: Please specify the string you want to search for.
  • Starting_Position: This is an optional parameter. If you want to specify the starting point (starting index position) then, Please specify the index value here. If you Omit this parameter then, the string endswith Function considers Zero as a starting position.
  • Ending_Position: This is an optional parameter. If you want to specify the endpoint (Ending index position) then, Please specify the index value here. If you omit this parameter, the endswith Function considers the highest index number.

Python endswith method Example

The following set of examples help you understand the endswith Function in Python.

# endswith Method Example

Str1 = 'Learn Python at Tutorial Gateway';
Str2 = Str1.endswith('Gateway')
print('First Output of a EndsWith() method is = ', Str2)

# Performing EndsWith() function directly
Str3 = 'Find Tutorial at Tutorial Gateway'.endswith('Gateway')
print('Second Output of a EndsWith() method is = ', Str3)

# Using First Index while finding the String
Str5 = Str1.endswith('Python', 6, 12)
print('Third Output of a EndsWith() method is = ', Str5)

# Searching for Non existing String
Str7 = Str1.endswith('Python')
print('Fourth Output of a EndsWith() method is = ', Str7)

OUTPUT

Python EndsWith

ANALYSIS

The following statement checks whether the string Str1 ended with the term ‘Gateway’ or not using endswith function and prints the output.

Str2 = Str1.endswith('Gateway')
print('First Output of a EndsWith() method is = ', Str2)

The endswith function allows us to use it directly on a string literal.

Str3 = 'Find Tutorial at Tutorial Gateway'.endswith('Gateway')
print('Second Output of a EndsWith() method is = ', Str3)

The string endswith function allows us to use Starting and ending indices. By specifying the starting and ending index positions, we can increase performance. The following Python statement starts looking from position 6 and checks whether the keyword ‘Python’ is at position 12 or not.

Str5 = Str1.endswith('Python', 6, 12)
print('Third Output of a EndsWith() method is = ', Str5)

If the string endswith function does not find the specified string at the ending point, it returns Boolean False. The following statement is returning Flase because this String Method starts looking for the term ‘Python’, and we all know that Str1 is ending with ‘Gateway’.

Str7 = Str1.endswith('Python')
print('Fourth Output of a EndsWith() method is = ', Str7)

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python Numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python Numpy concatenate
  • Python Numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python Substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy