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 in Python by Arvind
• 2,680 points
19 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 by Neel
• 2,690 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 in Python by Srisha
49 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 in Python by Sheik janibasha
48 views
0 votes
1 answer

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

You can use split() using space as ...READ MORE

answered Jun 1, 2018 in Python by george
• 200 points
1,565 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
• 56,520 points
2,189 views
+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 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 172 views
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 in Python by Neel
• 2,690 points
28 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 in Python by Neel
• 2,690 points
26 views