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

0 votes
I want to check whether an input string matches a particular pattern of characters. How to do it in Python?
Jul 3, 2019 in Python by Arvind
• 3,050 points
1,200 views

1 answer to this question.

0 votes

You can use re module of python for matching a specific pattern. re stands for regular expressions. For example

[A-Z] will match exactly one character between A and Z

\d+ will match one or more digits

() group things (and also return things... but for now just think of them grouping)

+ selects 1 or more

You can refer to the following code to understand it better

import re
pattern = re.compile("^([A-Z][0-9]+)+$")
pattern.match(string)
answered Jul 3, 2019 by Neel
• 3,020 points

Related Questions In Python

+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,088 views
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,876 views
0 votes
1 answer

How to check if a substring is present in a string using Python?

To check if the substring exists in ...READ MORE

answered May 9, 2019 in Python by Sharan
1,141 views
+1 vote
1 answer

How to check if a string is in a file, using python?

Hi, Try the below given code: with open('myfile.txt') as ...READ MORE

answered Jul 5, 2019 in Python by Shabnam
• 930 points
2,029 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
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,070 views
0 votes
1 answer

how to check whether a string starts with a particular word?

For this, you can use the built-in ...READ MORE

answered Jul 17, 2019 in Python by Neel
• 3,020 points
708 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,028 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