Python String startswith and endswith

Sometimes we need to check the starting or the ending part of any string for the programming purpose.  There are two built-in methods in Python to do the task. These are startswith() and endswith() methods. If any string starts with a given prefix then startswith() method will return true otherwise returns false and if any string ending with a given suffix then endswith() method will return true otherwise returns false. How these methods work and use in Python are shown in this tutorial. Spyder3 editor is used here to write and run the python script.

startswith() method:

You can search any sub-string from the beginning or a particular position of the string by using this method.

Syntax:

string.startswith( prefix [, start [, end]] )

Here, the prefix is the mandatory parameter of this method that will specify the substring that you want to search. The other two parameters are optional. start parameter is used to specify the starting position of the string from where the search will start and end parameter is used to specify the ending position of the string to stop the search. The uses of this method are shown below.

Example-1: Use startswith() to search particular strings

Create a python file with the following script to know the uses of startswith() method. In the first output, the method is called with the searching text only. In the second and third outputs, the method is called with the searching text, starting position and ending position. In the third output, the method is called with a searching text of multiple words.

#!/usr/bin/env python3
# Define the text
virusStatus = "Currently no vaccine is available to prevent coronavirus disease"

# Check the substring exists in the zero position or not
print("Output-1:", virusStatus.startswith('Current'))

# Check the substring exists in the particular positions
print("Output-2:", virusStatus.startswith('vaccine',13,30))

# Check the substring exists in the particular positions
print("Output-3:", virusStatus.startswith('corona',40,55))

# Check the string of multiple words exist in the partcular positions
print("Output-4:", virusStatus.startswith('prevent coronavirus',37,65))

Output:

The output is shown on the right side of the image. The first output is true because the ‘Current’ word exists in the variable, virusStatus. The second output is true because the ‘vaccine‘ word exists in position 13. The third output is false because ‘corona’ not exists within the position 48 to 55. The fourth output returns true because ‘prevent coronavirus’ exists within the position 37 to 65.

Example-2: Use startswith() to search tuple of strings

Create a python file with the following script to search string in the tuple using startswith() method. Here, startswith() method is used to search string without any position, with starting position and, with starting and ending positions.

#!/usr/bin/env python3

# Define the text
virusStatus = "Currently no vaccine is available to prevent coronavirus disease"

# Check the any string of the tuple exists in the zero position or not
print("Output-1:", virusStatus.startswith(('vaccine', 'coronavirus','available')))

# Check the any string of the tuple exists in the particular positions
print("Output-2:", virusStatus.startswith(('vaccine', 'coronavirus'),13))

# Check the any string of the tuple exists in the particular positions
print("Output-3:", virusStatus.startswith(('prevent','is', 'disease'), 21,60))

Output:

The output is shown on the right side of the image. The first output is false because none of the string of the tuple exists at the beginning of the text. The second output is true because the tuple value, ‘vaccine’ exists in the position, 13. The third output is true because the tuple value, ‘is’ exists in the position, 21.

endswith() method:

endswith() method works like startswith() method but it starts searching from the end of the string.

Syntax:

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

the suffix is a mandatory parameter here and it specifies the sub-string that will be searched from the end of the string. If you want to search from the specific position from the end of the string then you can use start and end parameters. The uses of this method are shown below.

Example-3: Use endswith() to search particular strings

Create a python file with the following script. Here, endswith() method is called for five times without position value, with only starting position value and with both stating and ending position values.

#!/usr/bin/env python3

text = "COVID-19 is an infectious disease caused by a newly discovered coronavirus"

# Check the substring exists in the last position of the text or not
print("Output-1:", text.endswith('coronavirus'))

# Check the substring exists in the particular positions
print("Output-2:", text.endswith('virus',40))

# Check the substring exists in the particular positions
print("Output-3:", text.endswith('disease',10,33))

# Check the string of multiple words exist in the partcular positions
print("Output-4:", text.endswith('newly discovered',30,62))

# Check the string of multiple words exist in the partcular positions
print("Output-5:", text.endswith('newly discovered',30,62))

Output:

The output is shown on the right side of the image. The first output is true because the string, ‘coronavirus’ exists at the end of the string.  The second output is true because the string, ‘virus’ exists at the end of the text if you start the search from the position 40. The third output is true because the string, ‘disease’ exists at the end position if you search it from the position  10 to 33. The fourth output is true because the string, ‘newly discovered’ exists at the end position if you search it from the position 30 to 62. The fifth output is false because the string, ‘coronavirus’ doesn’t exist at the end position.

Example-4: Use endswith() to search tuple of strings

Create a python file with the following code to search any string value from a tuple in a text by using endswith() method. This method is called three times in the script without position value and with the position values.

#!/usr/bin/env python3

text = "COVID-19 is an infectious disease caused by a newly discovered coronavirus"

# Check the any string of the tuple exists in the last position of the string or not
print("Output-1:", text.endswith(('COVID-19', 'coronavirus','available')))

# Check the any string of the tuple exists in the particular positions
print("Output-2:", text.endswith(('discovered', 'coronavirus'),13))

# Check the any string of the tuple exists in the particular positions
print("Output-3:", text.endswith(('infectious','is', 'disease'), 21,60))

Output:

The output is shown on the right side of the image. The first output is true because the string, ‘coronavirus’ exists at the end of the string.  The second output is true because the string, ‘coronavirus’ exists at the end of the text if you start the search from position 13. The third output is false because none of the tuple value exists at the end position of the text if you search within the position 21 to 60.

Conclusion:

It is very easy to search a particular string from the starting and the ending of a long text by using startswith() and endswith() methods in Python. I hope, this tutorial will help the reader to understand the uses of these methods properly.



from Linux Hint https://ift.tt/2UYOZ4t

Post a Comment

0 Comments