Tuesday, October 6, 2015

Python Tutorial: Python Startswith String Method - Python Strings #71



 Learn more at our website http://learnpythontutorial.com/python-startswith-string-method/



Python Startswith String Method

Python Startswith String Method

In this Python Tutorial, we will take a look at the Python startswith string method. The startswith string method allow us to check if a string object starts with certain characters. The startswith string method has one mandatory argument which must be in a string format this argument is matched the start of the string object. Unlike strip string methods, this argument must match the order and is also case-sensitive. If startswith string method finds a, match then we will be returned a boolean of True and if it does not match then we are returned a boolean of False. We also have two optional arguments which are the start index position and the end index position. The default start index position is the start of the string object and the default end position is the end of the string object.



Python Startswith String Method Syntax

'String Object'.startswith('substring', start, stop)



'String Object' - This is our string object which we are going to call the startswith string method on.

.startswith() - This string method will check if the string object starts with the startswith the substring argument and if it matches then we are returned True and if it does not match then we are returned False.

'substring' - This is the argument that we provide the string method. This argument will be searched through the string object if there is a match then we are returned True and if it does not match then we are returned False. The argument is case-sensitive and the order must also match.

start - The start argument will indicate where the startswith string method will start the search for an argument match. This argument defaults to the start of the string.

stop - The stop argument will indicate where the the search will end. The default value is the end of the string object.

Examples Of The Python Startswith String Method

Example 1:

 'This is a string'.startswith('This')

True



Example 2:

 'A new string'.startswith('new', 2)

True



Example 3:

 'My String'.startswith('string', 3, 9)

False

Examples Explained



Example 1:



'This is a string'.startswith('This') - in this example we created new string object and call the startswith string method on our string object.  we provide one argument to our string method ('This').



True -  We are return True because this startswith string method found 'This' in the beginning of the string object.

Example 2:



'A new string'.startswith('new', 2) - We create a new string object and then we call this startswith string method on the string object and then we provide two arguments first argument is 'new' and the second argument is the starting point for our search which is second index position.



True - We are returned True because the Python startswith string method located 'new' which starts at the Index position 2.

Example 3:



'My String'.startswith('string', 3, 9) - We create a new string object and then we call startswith string method on our string object. We provide three arguments first argument is 'first'. Second argument is index position 3 and the third argument is index position 9.



Conclusion



In this Python tutorial we take a look at the Python startswith string method. If you have any questions leave a comment below.

No comments:

Post a Comment