how to check whether a string starts with a particular word

0 votes
For example, I would like to know how to check whether a string starts with "Morning" in Python. how to do it in an easy way?
Jul 17, 2019 in Python by Arvind
• 3,050 points
708 views

1 answer to this question.

0 votes

For this, you can use the built-in method called startswith(). For example,

mystring = "Morning"
mystring.startswith("Morning") # This line will return true

Also you can use the re module.

import re

regex=re.compile('^Morning')
## regex=re.compile('^Good|^Morning') # used if you have multiple words

mystring="Morning, How are u?"

if re.match(regex, mystring):
    print("Yes")
answered Jul 17, 2019 by Neel
• 3,020 points

Related Questions In Python

0 votes
1 answer

How to check if a string ends with a character in python?

You can use the endswith method. It checks ...READ MORE

answered Apr 5, 2019 in Python by Srisha
2,877 views
0 votes
2 answers

How to check whether a string contains alphabets or number in Python?

By using isAlpha () to check whether ...READ MORE

answered Jul 14, 2019 in Python by Sheik janibasha
5,389 views
+1 vote
2 answers

How to print first character of each word in upper case of a string in Python

class Solution:     def firstAlphabet(self, s):             self.s=s              k=''              k=k+s[0]              for i in range(len(s)):                     if ...READ MORE

answered Oct 28, 2020 in Python by Anurag
12,389 views
+1 vote
1 answer

How to check if a string is null in python

Try this: if cookie and not cookie.isspace(): # the ...READ MORE

answered Aug 20, 2018 in Python by Priyaj
• 58,020 points
23,091 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
0 votes
1 answer

How to check if a given string matches a particular pattern in Python?

You can use re module of python ...READ MORE

answered Jul 3, 2019 in Python by Neel
• 3,020 points
1,201 views
0 votes
1 answer

How to print a string with curly braces in Python?

You can use something like this - mystring ...READ MORE

answered Jul 23, 2019 in Python by Neel
• 3,020 points
12,029 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP