Advertisement



< Prev
Next >



Python - String startswith() Method




The startswith() method available to string objects determines if a string object starts with a specific string value or not and if it does, the bool value True is returned, else the bool value False is returned.




Signature of startswith() method


startswith(value, start, end)


Parameters Description
value
This is a string value, which is searched in the invoked string. A non-optional attribute.

start
This is an integer value to specify the index position(zero-based index) to start the search at. An optional attribute.

end
An integer value to specify the index position(one-based index) to end the search at. An optional attribute.







In the upcoming example, we are going to call the startswith() with two optional parameters start and end.

# Python - Example of startswith() method.


# Creating a string 
s1 = 'Always think positive!'



# Checking if the string has a value 'always', starting at index 0 and ending at 15
# which is true because the value 'always' starts at index 0
b1 = s1.startswith('Always', 0, 15)

print(b1)


# Checking if the string has a value 'think', starting at index 7 and ending at 20
# which is true because the value 'think' starts at index 7
b1 = s1.startswith('think', 7, 20)

print(b1)  



# Checking if the string starts with the value 'ways', starting at index 2 and ending at 10
# which is true because the value 'ways' starts at the index 2
b2 = s1.startswith('ways', 2, 10)

print(b2)



# Checking if the string starts with the value 'positive, starting at index 5 and ending at index 10
# which is false because the value 'positive' does not start at the index 5
b3 = s1.startswith('positive', 5, 10)
print(b3)


Output is :


True
True
True
False


Program Analysis





Please share this article -




< Prev
Next >
< String isprintable() Method
String endswith() Method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON and DJANGO


Advertisement