TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python String | startswith() method with Examples

Python String startswith() method with Examples on capitalize(), center(), count(), encode(), find(), format(), index(), join(), lower(), ljust(), isupper(), istitle(), isspace(), isprintable(), isnumeric(), islower() etc.

<< Back to PYTHON

Python String startswith() Method

Python startswith() method returns either True or False. It returns True if the string starts with the prefix, otherwise False. It takes two parameters start and end. Start is a starting index from where searching starts and end index is where searching stops.

Signature

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

Parameters

prefix : A string which is to be checked.

start : Start index from where searching starts.

end : End index till there searching performs.

Both start and end are optional parameters.

Return

It returns boolean value either True or False.

Let's see some examples of startswith() method to understand it's functionality.

Python String startswith() Method Example 1

Let's first create a simple example which prints True if the string starts with the prefix.

# Python String startswith() method
# Declaring variable
str = "Hello TheDeveloperBlog"
# Calling function
str2 = str.startswith("Hello")
# Displaying result
print (str2)

Output:

True

Python String startswith() Method Example 2

If the string does not start with prefix, the method returns False. See the example below

# Python String startswith() method
# Declaring variable
str = "Hello TheDeveloperBlog"
# Calling function
str2 = str.startswith("Java") # False
# Displaying result
print (str2)

Output:

False

Python String startswith() Method Example 3

This method takes three parameters. The start and end index are optional. Here, we are passing start index only.

# Python String startswith() method
# Declaring variable
str = "Hello TheDeveloperBlog"
# Calling function
str2 = str.startswith("Java",6)
# Displaying result
print (str2)

Output:

True

Python String startswith() Method Example 4

It returns true if the string lie between start and end index starts from the prefix. An example is created to describe the process.

# Python String startswith() method
# Declaring variable
str = "Hello TheDeveloperBlog"
# Calling function
str2 = str.startswith("Java",6,10)
# Displaying result
print (str2)
str2 = str.startswith("Java",8,12)
# Displaying result
print (str2)

Output:

True
False

Next TopicPython Strings




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf