Python Tutorial- Startswith and Endswith Function

Python Exercise Lists

In addition to the functions described earlier, Python string variables can also use the startswith and endswith functions.

Python Startswith Function

The startswith() function is used to retrieve whether the string starts with the specified string, and returns True if it is; otherwise it returns False. The syntax of this method is as follows:

str.startswith(sub[,start][,end]))

The specific meanings of the parameters in this format are as follows:

  • str: the original string;
  • sub: the substring to be retrieved;
  • start: Specifies the index of the starting position of the search. If not specified, the search starts from the beginning by default;
  • end: Specifies the index of the end position of the search. If not specified, the search is always ended at the end.

[Example:] Determine whether "www.freelearningpoints.com" starts with "w" substring.

string = "www.freelearningpoints.com"
string.startswith("w")

The output is:

True

[Example:]

string = "www.freelearningpoints.com"
string.startswith("a")

The output is:

False

[Example] Search from the specified position.

string = "www.freelearningpoints.com"
string.startswith("f", 4)

The output is:

True

Python Endswith Function

The endswith() function is used to retrieve whether the string ends with the specified string, and returns True if it is; otherwise it returns False. The syntax of the method is as follows:

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

The specific meanings of the parameters in this format are as follows:

  • str: the original string;
  • sub: the string to be retrieved;
  • start: Specifies the index of the starting position at the beginning of the search (the index value of the first character of the string is 0). If not specified, the search starts from the beginning by default.
  • end: Specifies the index of the end position of the search. If not specified, the search is performed until the end by default.

[Example:] Search whether "www.freelearningpoints.com" ends with "com".

string = "www.freelearningpoints.com"
string.endswith("com")

The output is:

True

                               


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression


More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises