Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python String strip() Method
Next article icon

Python – String startswith()

Last Updated : 29 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

startswith() method in Python checks whether a given string starts with a specific prefix. It helps in efficiently verifying whether a string begins with a certain substring, which can be useful in various scenarios like:

  • Filtering data
  • Validating input
  • Simple pattern matching

Let’s understand by taking a simple example:

[GFGTABS]
Python

s = "GeeksforGeeks" res = s.startswith("for")  print(res)   


[/GFGTABS]

Output
False 

Explanation: startswith() method checks if the string s starts with “for“, and since it starts with “Geeks” instead of for, the result is False.

Syntax

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

Parameters:

  • prefix: A string or a tuple of strings to check at the start.
  • start (optional): Index to start checking from.
  • end (optional): Index to stop checking.

Return Type: The method returns a Boolean:

  • True if the string starts with the specified prefix.
  • False if it does not.

Examples of startswith() method

Example 1: Basic Prefix Check

Here’s a simple example where we check if a given string starts with the word “for”.

[GFGTABS]
Python

s = "GeeksforGeeks"  res = s.startswith("for") print(res)   


[/GFGTABS]

Output
False 

Explanation: The string starts with “Geeks“, so it returns True.

Example 2: Using start Parameter

Let’s check the string using the start parameter to begin from a specific index.

[GFGTABS]
Python

s = "GeeksforGeeks" print(s.startswith("for", 5)) 


[/GFGTABS]

Output
True 

Explanation: We’re checking from index 5, and “for” starts from there hence the result is True.

Example 3: Using start and end Parameters

We can also check for stings within a given slice using star and end parameters.

[GFGTABS]
Python

s = "GeeksforGeeks" print(s.startswith("for", 5, 8)) 


[/GFGTABS]

Output
True 

Explanation: checking only between index 5 and 8 (non inclusive) and “for” lies in that slice, so it returns True.

Example 4: Checking Multiple Prefixes

We can also check if the string starts with any one of several prefixes by passing a tuple of prefixes.

[GFGTABS]
Python

s = "GeeksforGeeks"  res = s.startswith(("Geeks", "G")) print(res) 


[/GFGTABS]

Output
True 

Explanation:

  • We pass a tuple with two prefixes: “Geeks” and “G”.
  • The string starts with “Geeks“, which is one of the options in the tuple, so the result is True.

Next Article
Python String strip() Method
author
pawan_asipu
Improve
Article Tags :
  • Python
  • python-string
Practice Tags :
  • python

Similar Reads

  • Python String istitle() Method
    The istitle() method in Python is used to check whether a string follows the title case formatting. In a title-cased string, the first letter of each word is capitalized, and all other letters in the word are in lowercase. This method is especially useful when working with formatted text such as tit
    3 min read
  • Python String isupper() method
    isupper() method in Python checks if all the alphabetic characters in a string are uppercase. If the string contains at least one alphabetic character and all of them are uppercase, the method returns True. Otherwise, it returns False. Let's understand this with the help of an example: [GFGTABS] Pyt
    3 min read
  • Python String join() Method
    The join() method in Python is used to concatenate the elements of an iterable (such as a list, tuple, or set) into a single string with a specified delimiter placed between each element. Lets take a simple example to join list of string using join() method. Joining a List of StringsIn below example
    3 min read
  • String lower() Method in Python
    lower() method in Python converts all uppercase letters in a string to their lowercase. This method does not alter non-letter characters (e.g., numbers, punctuation). Let's look at an example of lower() method: [GFGTABS] Python s = "HELLO, WORLD!" # Change all uppercase letters to lowercas
    3 min read
  • Python String lstrip() Method
    The lstrip() method removes leading whitespace characters from a string. We can also specify custom characters to remove from the beginning/starting of the string. Let's take an example to remove whitespace from the starting of a string. [GFGTABS] Python s = " Hello Python!" res = s.lstrip
    2 min read
  • Python String partition() Method
    In Python, the String partition() method splits the string into three parts at the first occurrence of the separator and returns a tuple containing the part before the separator, the separator itself, and the part after the separator. Let's understand with the help of an example: [GFGTABS] Python s
    3 min read
  • Python String replace() Method
    The replace() method replaces all occurrences of a specified substring in a string and returns a new string without modifying the original string. Let’s look at a simple example of replace() method. [GFGTABS] Python s = "Hello World! Hello Python!" # Replace "Hello" with "Hi
    2 min read
  • Python String rfind() Method
    Python String rfind() method returns the rightmost index of the substring if found in the given string. If not found then it returns -1. Example[GFGTABS] Python s = "GeeksForGeeks" print(s.rfind("Geeks")) [/GFGTABS]Output8 Explanation string "GeeksForGeeks" contains the substring
    4 min read
  • Python String rindex() Method
    Python String rindex() method returns the highest index of the substring inside the string if the substring is found. Otherwise, it raises ValueError. Let's understand with the help of an example: [GFGTABS] Python s = 'geeks for geeks' res= s.rindex('geeks') print(res) [/GFGTABS]Outp
    2 min read
  • Python String rpartition() Method
    String rpartition() method in Python is a powerful tool for splitting a string into three parts, based on the last occurrence of a specified separator. It provides an efficient way to handle text parsing when we want to divide strings from the rightmost delimiter. Let us start with a simple example:
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences