LearnFrenzy
Sign in Take a free test

Python Programming · Topic 22 of 25

Regular Expressions Questions and Concepts

  • 4 exercises
  • 50 questions
  • ~60 min
  • Answers explained

Coding Techniques

Regular Expressions · Study material

Compile once, reuse everywhere

A compiled pattern with anchors validates input reliably. Here: an optional +91 prefix, then a 10-digit Indian mobile number starting with 6–9.

import re

PHONE = re.compile(r"^(?:\+91[-\s]?)?[6-9]\d{9}$")     # compile once, reuse many times

numbers = ["9845012345", "+91 9845012345", "+91-7012345678", "5845012345", "98450"]
for n in numbers:
    print(f"{n:<16} {'valid' if PHONE.match(n) else 'invalid'}")

Output

9845012345       valid
+91 9845012345   valid
+91-7012345678   valid
5845012345       invalid
98450            invalid

Handy patterns

GoalPattern
Whole number^-?\d+$
Decimal number^-?\d+(\.\d+)?$
Indian PIN code^[1-9]\d{5}$
Simple email^[\w.+-]+@[\w-]+\.[\w.]+$
Date DD-MM-YYYY^\d{2}-\d{2}-\d{4}$
Hashtags#\w+
Repeated words\b(\w+) \1\b
Extra spaces\s+ → " "

Safe matching pattern

import re

m = re.search(r"Order #(\d+)", text)
if m:                                   # a failed search returns None
    order_id = int(m.group(1))
else:
    order_id = None

order_id = int(m.group(1)) if (m := re.search(r"Order #(\d+)", text)) else None   # same with :=

Regex checklist

  • Use raw strings r"…".
  • Use fullmatch (or ^…$) for validation.
  • Check for None before calling .group().
  • Escape literal dots and brackets; use re.escape for user text.
  • Prefer lazy quantifiers or negated classes over greedy .*.
  • Use named groups for readable extraction.
  • Use re.VERBOSE to comment long patterns.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About Python Programming

Why it matters

  • Python is the most popular first language and the default for data science, AI, automation and scripting
  • Campus coding rounds and technical interviews increasingly allow – or prefer – Python
  • "Predict the output" questions on lists, strings, dictionaries and mutable defaults are a favourite in written tests
  • Clean Python with comprehensions, generators and OOP is what product companies expect from freshers

Who it is for

  • School students (Class 11–12 computer science with Python) preparing for board exams and practicals
  • College students (B.Tech, BCA, MCA, B.Sc CS) preparing for semester exams, lab work and viva
  • Freshers preparing for campus placements, coding tests and Python interviews
  • Anyone moving into data science, AI or automation who needs strong Python fundamentals

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • Topic-wise MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs: syntax errors, runtime exceptions and logical bugs
  • Coding problems with solutions and real output, every one run on Python 3 – open any example in the online Python compiler

Asked in

  • Campus placements
  • TCS, Infosys, Wipro, Accenture tests
  • Python interviews for freshers
  • Data science interviews
  • CBSE / university practicals
  • GATE CS

How to use this section

  1. Read the study material of a topic and run the examples in the browser.
  2. Practise the four exercises – MCQs, output, errors, coding problems – each marked Easy, Medium or Hard, and read every explanation.
  3. Revise with the interview questions and cheat sheet, then move to the next topic.

Search LearnFrenzy

Esc

Quick links