

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check whether a string starts with XYZ in Python?
Python has a method startswith(string) in the String class. This method accepts a prefix string that you want to search and is called on a string object. You can call this method in the following way:
>>>'hello world'.startswith('hell') True >>>'hello world'.startswith('nope') False
There is another way to find if a string ends with a given prefix. You can use re.search('^' + prefix, string) from the re module(regular expression) to do so. Regex interprets ^ as start of line, so if you want to search for a prefix,you need to do the following:
>>>import re >>>bool(re.search('^hell', 'hello world')) True >>>bool(re.search('^nope', 'hello world')) False
- Related Questions & Answers
- Python - Check whether a string starts and ends with the same character or not
- How to check if string or a substring of string starts with substring in Python?
- How to check if a string starts with a specified Prefix string in Golang?
- Python Program to check if a string starts with a substring using regex
- Check if a string starts with given word in PHP
- Java Program to check if any String in the list starts with a letter
- How to check whether a string ends with one from a list of suffixes in Python?
- Check whether a string ends with some other string - JavaScript
- Check if a String starts with any of the given prefixes in Java
- How to Check Whether a String is Palindrome or Not using Python?
- How can I test if a string starts with a capital letter using Python?
- How to check whether a string contains a substring in jQuery?
- How to check whether a string contains a substring in JavaScript?
- How do we check in Python whether a string contains only numbers?
- Program to check whether string Is transformable with substring sort operations in Python
Advertisements