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,000 points
138 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 4, 2019 in Python by Srisha
1,308 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 13, 2019 in Python by Sheik janibasha
3,006 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
7,838 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,120 points
20,044 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 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,184 views
0 votes
0 answers
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
371 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
5,425 views