How to Use String startswith() Method in PythonPython Basics


The startswith() method is used to check if a string starts with a prefix (the specified string).

Note: the check is case sensitive.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

str = "Have a nice day!"
print(str.startswith('Have'))
print(str.startswith('a', 5))
print(str.startswith('A', 5))

Output:

True
True
False

Syntax

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

Parameters

Name Description
prefix The string to be checked
start Beginning position where prefix is to be checked within the string
end Ending position where prefix is to be checked within the string

Return Value

It returns true or false.

© 2024 Learnwithgpt.org, all rights reserved. Privacy Policy | Contact Us