What is the startswith( ) method in Python?

The startswith() method tells whether a certain string A starts with a string B.

Returns a boolean value
Returns a boolean value
string_A = "What do i begin with?"
string_B = "What"
print ( string_A.startswith(string_B) ) # startswith() call

Optionally, you can add 2 more arguments. This gives you the power to see whether a very specific part within a string A starts with a string B.

These arguments are the starting and ending indices of this “specific part”.

svg viewer
string_A = "What do i begin with?"
string_B = "at"
print ( string_A.startswith(string_B, 2, 6) ) #startswith() call

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved