Untitled
3 years ago in Plain Text
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Python - Regular Expressions - Tutorialspoint</title>
<meta charset="utf-8">
<meta name="description" content="Python - Regular Expressions - A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pat"/>
<meta name="keywords" content="C, C++, Python, Java, HTML, CSS, JavaScript, SQL, PHP, jQuery, XML, DOM, Bootstrap, Tutorials, Articles, Programming, training, learning, quiz, preferences, examples, code"/>
<link rel="canonical" href="https://www.tutorialspoint.com/python/python_reg_expressions.htm" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=yes">
<link rel="stylesheet" href="/themes/css/style-min-v1.css?v=3.1.2.336">
<script src="/themes/js/script-min-v1.js?v=3.0.2040229"></script>
<script>
function printPage() { window.print(); }
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
</script>
<script id="adp1" data-cfasync="false" type="text/javascript">(function(w, d) { var s = d.createElement('script'); s.src = '//cdn.adpushup.com/40046/adpushup.js'; s.type = 'text/javascript'; s.async = true; (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s); })(window, document);</script>
<script id="adp2" data-cfasync="false" type="text/javascript">(function(w, d) {var cval = readCookie('subscription_purchased');if(cval == '1'){ return; } var s = d.createElement('script'); s.src = '//delivery.adrecover.com/18107/adRecover.js'; s.type = 'text/javascript'; s.async = true; (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s); })(window, document);</script>
<style>
.butnn-save a{color:#fff;} .notes_msg{color:green; margin-left:20px; margin-top:15px;}
.right-menu .mui-btn {
background-color:#fdbf00;
}
a.demo {
background:#fdbf00;
}
li.heading {
background:#fdbf00;
}
.course-box{background:#fdbf00}
.home-intro-sub p{color:#fdbf00}
a.demo{
top: 12px;
float:right;
}
.prettyprint{
width: 99%;
}
pre.prettyprint.tryit {min-height:37px; background: #eee url(/python/images/try-it.jpg) top right no-repeat !important}</style>
</head>
<body>
<header id="header">
<!-- pop-up -->
<div class="pop-modal overlay-pop popdiv">
<div class="modal-window small">
<span class="close" title="close">×</span>
<div class="pop-content">
</div>
<span class="msg" ></span>
</div>
</div>
<div class="wrap_loader">
<div class="imgLoader"><img src="/videotutorials/images/loader.gif" alt="Tutorialspoint" width="70" height="70"></div>
</div>
<input type="hidden" id="vu" name="vu" value="" />
<!-- pop-up -->
<!-- Top sub-menu Starts Here -->
<div class="mui-appbar mui-container-fulid top-menu">
<div class="mui-container">
<div class="top-menu-item home">
<a href="https://www.tutorialspoint.com/index.htm" target="_blank" title="TutorialsPoint - Home"><i class="fal fa-home"></i> <span>Home</span></a>
</div>
<div class="top-menu-item qa">
<a href="https://www.tutorialspoint.com/about/about_careers.htm" target="_blank" title="Job @ Tutorials Point"><i class="fa fa-suitcase"></i> <span>Jobs</span></a>
</div>
<div class="top-menu-item tools">
<a href="https://www.tutorialspoint.com/online_dev_tools.htm" target="_blank" title="Tools - Online Development and Testing Tools"><i class="fal fa-cogs"></i> <span>Tools</span></a>
</div>
<div class="top-menu-item coding-ground">
<a href="https://www.tutorialspoint.com/codingground.htm" target="_blank" title="Coding Ground - Free Online IDE and Terminal"><i class="fal fa-code"></i> <span>Coding Ground </span></a>
</div>
<div class="top-menu-item current-affairs">
<a href="https://www.tutorialspoint.com/current_affairs.htm" target="_blank" title="Daily Current Affairs"><i class="fal fa-layer-plus"></i> <span>Current Affairs</span></a>
</div>
<div class="top-menu-item upsc-notes">
<a href="https://www.tutorialspoint.com/upsc_ias_exams.htm" target="_blank" title="UPSC IAS Exams Notes - TutorialsPoint"><i class="fal fa-user-tie"></i> <span>UPSC Notes</span></a>
</div>
<div class="top-menu-item articles">
<a href="https://www.tutorialspoint.com/tutor_connect/index.php" target="_blank" title="Top Online Tutors - Tutor Connect"><i class="fal fa-user"></i> <span>Online Tutors</span></a>
</div>
<div class="top-menu-item whiteboard">
<a href="https://www.tutorialspoint.com/whiteboard.htm" target="_blank" title="Free Online Whiteboard"><i class="fal fa-chalkboard"></i> <span>Whiteboard</span></a>
</div>
<div class="top-menu-item net-meeting">
<a href="https://www.tutorialspoint.com/netmeeting.php" target="_blank" title="A free tool for online video conferencing"><i class="fal fa-chalkboard-teacher"></i> <span>Net Meeting</span></a>
</div>
<div class="top-menu-item articles">
<a href="https://www.tutorix.com" target="_blank" title="Tutorx - The Best Learning App" rel="nofollow"><i class="fal fa-video"></i> <span>Tutorix</span></a>
</div>
<div class="top-menu-item qa login">
<a href="/videotutorials/login.php" target="_blank" title="Tutorials Point - Login"><i class="fa fa-sign-in-alt"></i> <span>Login</span></a>
</div>
<div class="top-menu-item qa login">
<a href="/videotutorials/subscription.php" target="_blank" title="Paid Package"><i class="fa fa-book-reader"></i> <span>Packages</span></a>
</div>
<div class="social-menu-item">
<a href="https://www.facebook.com/tutorialspointindia" target="_blank" rel="nofollow" data-placement="bottom" title="tutorialspoint @ Facebook"><i class="fab fa-facebook-f"></i></a>
<a href="https://www.twitter.com/tutorialspoint" target="_blank" rel="nofollow" data-placement="bottom" title="tutorialspoint @ Twitter"><i class="fab fa-twitter"></i></a>
<a href="https://www.linkedin.com/company/tutorialspoint" target="_blank" rel="nofollow" data-placement="bottom" title="tutorialspoint @ Linkedin"><i class="fab fa-linkedin-in"></i></a>
<a href="https://www.youtube.com/channel/UCVLbzhxVTiTLiVKeGV7WEBg" target="_blank" rel="nofollow" data-placement="bottom" title="tutorialspoint YouTube"><i class="fab fa-youtube"></i></a>
</div>
</div>
</div>
<!-- Top sub-menu Ends Here -->
<!-- Top main-menu Starts Here -->
<div class="mui-appbar mui-container-fulid mui--appbar-line-height mui--z1" id="logo-menu">
<div class="mui-container">
<div class="left-menu">
<a href="https://www.tutorialspoint.com/index.htm" title="Tutorialspoint">
<img class="tp-logo" alt="tutorialspoint" src="/python/images/logo.png">
</a>
<div class="mui-dropdown">
<a class="mui-btn mui-btn--primary categories" data-mui-toggle="dropdown"><i class="fa fa-th-large"></i>
<span>Categories <span class="mui-caret"></span></span></a>
<ul class="mui-dropdown__menu cat-menu">
<li>
<ul>
<li><a href="/academic_tutorials.htm"><i class="fa fa-caret-right"></i> Academic Tutorials</a></li>
<li><a href="/big_data_tutorials.htm"><i class="fa fa-caret-right"></i> Big Data &amp; Analytics </a></li>
<li><a href="/computer_programming_tutorials.htm"><i class="fa fa-caret-right"></i> Computer Programming </a></li>
<li><a href="/computer_science_tutorials.htm"><i class="fa fa-caret-right"></i> Computer Science </a></li>
<li><a href="/database_tutorials.htm"><i class="fa fa-caret-right"></i> Databases </a></li>
<li><a href="/devops_tutorials.htm"><i class="fa fa-caret-right"></i> DevOps </a></li>
<li><a href="/digital_marketing_tutorials.htm"><i class="fa fa-caret-right"></i> Digital Marketing </a></li>
<li><a href="/engineering_tutorials.htm"><i class="fa fa-caret-right"></i> Engineering Tutorials </a></li>
<li><a href="/upsc_ias_exams.htm"><i class="fa fa-caret-right"></i> Exams Syllabus </a></li>
<li><a href="/famous_monuments.htm"><i class="fa fa-caret-right"></i> Famous Monuments </a></li>
<li><a href="/gate_exams_tutorials.htm"><i class="fa fa-caret-right"></i> GATE Exams Tutorials</a></li>
<li><a href="/latest_technologies.htm"><i class="fa fa-caret-right"></i> Latest Technologies </a></li>
<li><a href="/machine_learning_tutorials.htm"><i class="fa fa-caret-right"></i> Machine Learning </a></li>
<li><a href="/mainframe_tutorials.htm"><i class="fa fa-caret-right"></i> Mainframe Development </a></li>
<li><a href="/management_tutorials.htm"><i class="fa fa-caret-right"></i> Management Tutorials </a></li>
<li><a href="/maths_tutorials.htm"><i class="fa fa-caret-right"></i> Mathematics Tutorials</a></li>
<li><a href="/microsoft_technologies_tutorials.htm"><i class="fa fa-caret-right"></i> Microsoft Technologies </a></li>
<li><a href="/misc_tutorials.htm"><i class="fa fa-caret-right"></i> Misc tutorials </a></li>
<li><a href="/mobile_development_tutorials.htm"><i class="fa fa-caret-right"></i> Mobile Development </a></li>
<li><a href="/java_technology_tutorials.htm"><i class="fa fa-caret-right"></i> Java Technologies </a></li>
<li><a href="/python_technologies_tutorials.htm"><i class="fa fa-caret-right"></i> Python Technologies </a></li>
<li><a href="/sap_tutorials.htm"><i class="fa fa-caret-right"></i> SAP Tutorials </a></li>
<li><a href="/scripting_lnaguage_tutorials.htm"><i class="fa fa-caret-right"></i>Programming Scripts </a></li>
<li><a href="/selected_reading.htm"><i class="fa fa-caret-right"></i> Selected Reading </a></li>
<li><a href="/software_quality_tutorials.htm"><i class="fa fa-caret-right"></i> Software Quality </a></li>
<li><a href="/soft_skill_tutorials.htm"><i class="fa fa-caret-right"></i> Soft Skills </a></li>
<li><a href="/telecom_tutorials.htm"><i class="fa fa-caret-right"></i> Telecom Tutorials </a></li>
<li><a href="/upsc_ias_exams.htm"><i class="fa fa-caret-right"></i> UPSC IAS Exams </a></li>
<li><a href="/web_development_tutorials.htm"><i class="fa fa-caret-right"></i> Web Development </a></li>
<li><a href="/sports_tutorials.htm"><i class="fa fa-caret-right"></i> Sports Tutorials </a></li>
<li><a href="/xml_technologies_tutorials.htm"><i class="fa fa-caret-right"></i> XML Technologies </a></li>
<li><a href="/multi_language_tutorials.htm"><i class="fa fa-caret-right"></i> Multi-Language</a></li>
<li><a href="/questions_and_answers.htm"><i class="fa fa-caret-right"></i> Interview Questions</a></li>
</ul>
</li>
</ul>
<div class="clear"></div>
</div>
</div>
<div class="right-menu">
<div class="toc-toggle">
<a href="javascript:void(0);"><i class="fa fa-bars"></i></a>
</div>
<div class="mobile-search-btn">
<a href="https://www.tutorialspoint.com/index.htm"><i class="fal fa-search"></i></a>
</div>
<div class="search-box">
<script async src="https://cse.google.com/cse.js?cx=000263362972888060243:yfed6eu421q"></script>
<div class='gcse-search'></div>
</div>
<div class="menu-btn library-btn">
<a href="https://www.tutorialspoint.com/tutorialslibrary.htm"><i class="fal fa-cubes"></i> <span>Library</span></a>
</div>
<div class="menu-btn videos-btn">
<a href="https://www.tutorialspoint.com/videotutorials/index.php"><i class="fal fa-video"></i> <span>Videos</span></a>
</div>
<div class="menu-btn videos-btn">
<a href="https://www.tutorialspoint.com/questions/index.php"><i class="fa fa-location-arrow"></i> <span>Q/A</span></a>
</div>
<div class="menu-btn ebooks-btn">
<a href="https://store.tutorialspoint.com"><i class="fal fa-book"></i> <span>eBooks</span></a>
</div>
<div class="mui-dropdown">
<button class="mui-btn mui-btn--primary mobile-menu" data-mui-toggle="dropdown">
<span class="mui-caret"></span>
</button>
<ul class="mui-dropdown__menu">
<li class="login"><a href="https://www.tutorialspoint.com/videotutorials/login.php"><i class="fal fa-sign-in-alt"></i> <span>Login</span></a></li>
<li><a href="https://www.tutorialspoint.com/tutorialslibrary.htm"><i class="fal fa-cubes"></i> <span>Library</span></a></li>
<li><a href="https://www.tutorialspoint.com/videotutorials/index.php"><i class="fal fa-video"></i> <span>Videos</span></a></li>
<li><a href="https://store.tutorialspoint.com"><i class="fal fa-book"></i> <span>eBooks</span></a></li>
</ul>
</div>
</div>
</div>
</div>
<!-- Top main-menu Ends Here -->
</header>
<div class="mui-container-fluid content">
<div class="mui-container">
<!-- Tutorial ToC Starts Here -->
<div class="mui-col-md-3 tutorial-toc">
<div class="mini-logo">
<img src="/python/images/python-mini.jpg" alt="Python Tutorial" />
</div>
<ul class="toc chapters">
<li class="heading">Python Basic Tutorial</li>
<li><a target="_top" href="/python/index.htm">Python - Home</a></li>
<li><a target="_top" href="/python/python_overview.htm">Python - Overview</a></li>
<li><a target="_top" href="/python/python_environment.htm">Python - Environment Setup</a></li>
<li><a target="_top" href="/python/python_basic_syntax.htm">Python - Basic Syntax</a></li>
<li><a target="_top" href="/python/python_variable_types.htm">Python - Variable Types</a></li>
<li><a target="_top" href="/python/python_basic_operators.htm">Python - Basic Operators</a></li>
<li><a target="_top" href="/python/python_decision_making.htm">Python - Decision Making</a></li>
<li><a target="_top" href="/python/python_loops.htm">Python - Loops</a></li>
<li><a target="_top" href="/python/python_numbers.htm">Python - Numbers</a></li>
<li><a target="_top" href="/python/python_strings.htm">Python - Strings</a></li>
<li><a target="_top" href="/python/python_lists.htm">Python - Lists</a></li>
<li><a target="_top" href="/python/python_tuples.htm">Python - Tuples</a></li>
<li><a target="_top" href="/python/python_dictionary.htm">Python - Dictionary</a></li>
<li><a target="_top" href="/python/python_date_time.htm">Python - Date &amp; Time</a></li>
<li><a target="_top" href="/python/python_functions.htm">Python - Functions</a></li>
<li><a target="_top" href="/python/python_modules.htm">Python - Modules</a></li>
<li><a target="_top" href="/python/python_files_io.htm">Python - Files I/O</a></li>
<li><a target="_top" href="/python/python_exceptions.htm">Python - Exceptions</a></li>
</ul>
<ul class="toc chapters">
<li class="heading">Python Advanced Tutorial</li>
<li><a target="_top" href="/python/python_classes_objects.htm">Python - Classes/Objects</a></li>
<li><a target="_top" href="/python/python_reg_expressions.htm">Python - Reg Expressions</a></li>
<li><a target="_top" href="/python/python_cgi_programming.htm">Python - CGI Programming</a></li>
<li><a target="_top" href="/python/python_database_access.htm">Python - Database Access</a></li>
<li><a target="_top" href="/python/python_networking.htm">Python - Networking</a></li>
<li><a target="_top" href="/python/python_sending_email.htm">Python - Sending Email</a></li>
<li><a target="_top" href="/python/python_multithreading.htm">Python - Multithreading</a></li>
<li><a target="_top" href="/python/python_xml_processing.htm">Python - XML Processing</a></li>
<li><a target="_top" href="/python/python_gui_programming.htm">Python - GUI Programming</a></li>
<li><a target="_top" href="/python/python_further_extensions.htm">Python - Further Extensions</a></li>
</ul>
<ul class="toc chapters">
<li class="heading">Python Useful Resources</li>
<li><a target="_top" href="/python/python_questions_answers.htm">Python - Questions and Answers</a></li>
<li><a target="_top" href="/python/python_quick_guide.htm">Python - Quick Guide</a></li>
<li><a target="_top" href="/python/python_tools_utilities.htm">Python - Tools/Utilities</a></li>
<li><a target="_top" href="/python/python_useful_resources.htm">Python - Useful Resources</a></li>
<li><a target="_top" href="/python/python_discussion.htm">Python - Discussion</a></li>
</ul>
<ul class="toc reading">
<li class="sreading">Selected Reading</li>
<li><a target="_top" href="/upsc_ias_exams.htm">UPSC IAS Exams Notes</a></li>
<li><a target="_top" href="/developers_best_practices/index.htm">Developer's Best Practices</a></li>
<li><a target="_top" href="/questions_and_answers.htm">Questions and Answers</a></li>
<li><a target="_top" href="/effective_resume_writing.htm">Effective Resume Writing</a></li>
<li><a target="_top" href="/hr_interview_questions/index.htm">HR Interview Questions</a></li>
<li><a target="_top" href="/computer_glossary.htm">Computer Glossary</a></li>
<li><a target="_top" href="/computer_whoiswho.htm">Who is Who</a></li>
</ul>
<div id="stickyparent">
<div style="height:250px;width:300px;" id="sticky-ad">
<div id="430c9911-782f-495d-88f9-69f58ca8c9a4" class="_ap_apex_ad" >
<script>
var adpushup = adpushup || {};
adpushup.que = adpushup.que || [];
adpushup.que.push(function() {
adpushup.triggerAd("430c9911-782f-495d-88f9-69f58ca8c9a4");
});
</script>
</div>
</div>
</div>
</div>
<!-- Tutorial ToC Ends Here -->
<!-- Tutorial Content Starts Here -->
<div class="mui-col-md-6 tutorial-content">
<h1>Python - Regular Expressions</h1>
<hr />
<div id="google-top-ads">
<div class="top-ad-heading">Advertisements</div>
<div style="text-align:center" class="google-top-ads">
<div id="adp_top_ads" style="text-align:center!important;min-height:90px!important"></div>
<script>
var adPos = document.getElementById('adp_top_ads');
var ad_id_720 = "8014640d-fd34-4b2e-9b1b-cc67234a2325";
var ad_id_650 = "2d1bb2c4-eb72-471d-9c80-1bf5514728c1";
var ad_id_300 = "df94a1a9-ae2f-432d-9a92-d93823ff4503";
var width = window.innerWidth;
var ad_id = ad_id_720;
if( width <= 768 ){
ad_id = ad_id_300;
}else if( width <= 1435 ){
ad_id = ad_id_650;
}
adPos.innerHTML = '<div id="' + ad_id + '" class="_ap_apex_ad"></div>';
var adpushup = adpushup || {};
adpushup.que = adpushup.que || [];
adpushup.que.push(function() {
adpushup.triggerAd(ad_id);
});
</script>
</div>
</div>
<br>
<div class="mui-container-fluid button-borders">
<div class="pre-btn">
<a href="/python/python_classes_objects.htm"><i class="fal fa-chevron-circle-left"></i> Previous Page</a>
</div>
<div class="nxt-btn">
<a href="/python/python_cgi_programming.htm">Next Page <i class="fal fa-chevron-circle-right"></i>&nbsp;</a>
</div>
</div>
<div class="clearer"></div>
<p>A <i>regular expression</i> is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. Regular expressions are widely used in UNIX world.</p>
<p>The Python module <b>re</b> provides full support for Perl-like regular expressions in Python. The re module raises the exception re.error if an error occurs while compiling or using a regular expression.</p>
<p>We would cover two important functions, which would be used to handle regular expressions. But a small thing first: There are various characters, which would have special meaning when they are used in regular expression. To avoid any confusion while dealing with regular expressions, we would use Raw Strings as <b>r'expression'</b>.</p>
<h2>The <i>match</i> Function</h2>
<p>This function attempts to match RE <i>pattern</i> to <i>string</i> with optional <i>flags</i>.</p>
<p>Here is the syntax for this function &minus;</p>
<pre class="result notranslate">
re.match(pattern, string, flags=0)
</pre>
<p>Here is the description of the parameters &minus;</p>
<table class="table table-bordered">
<tr>
<th style="text-align:center;width:10%">Sr.No.</th>
<th style="text-align:center;">Parameter &amp; Description</th>
</tr>
<tr>
<td class="ts">1</td>
<td><p><b>pattern</b></p>
<p>This is the regular expression to be matched.</p></td>
</tr>
<tr>
<td class="ts">2</td>
<td><p><b>string</b></p>
<p>This is the string, which would be searched to match the pattern at the beginning of string.</p></td>
</tr>
<tr>
<td class="ts">3</td>
<td><p><b>flags</b></p>
<p>You can specify different flags using bitwise OR (|). These are modifiers, which are listed in the table below.</p></td>
</tr>
</table>
<p>The <i>re.match</i> function returns a <b>match</b> object on success, <b>None</b> on failure. We use<i>group(num)</i> or <i>groups() </i> function of <b>match</b> object to get matched expression.</p>
<table class="table table-bordered">
<tr>
<th style="text-align:center;width:10%">Sr.No.</th>
<th style="text-align:center;">Match Object Method &amp; Description</th>
</tr>
<tr>
<td class="ts">1</td>
<td><p><b>group(num=0)</b></p>
<p>This method returns entire match (or specific subgroup num)</p></td>
</tr>
<tr>
<td class="ts">2</td>
<td><p><b>groups()</b></p>
<p>This method returns all matching subgroups in a tuple (empty if there weren't any)</p></td>
</tr>
</table>
<h3>Example</h3>
<div class="demo-view">
<a href="http://tpcg.io/cZGpLX" target="_blank" rel="nofollow" class="demo"><i class="fa-external-link"></i> Live Demo</a>
</div>
<pre class="prettyprint notranslate">
#!/usr/bin/python
import re
line = "Cats are smarter than dogs"
matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I)
if matchObj:
print "matchObj.group() : ", matchObj.group()
print "matchObj.group(1) : ", matchObj.group(1)
print "matchObj.group(2) : ", matchObj.group(2)
else:
print "No match!!"
</pre>
<p>When the above code is executed, it produces following result &minus;</p>
<pre class="result notranslate">
matchObj.group() : Cats are smarter than dogs
matchObj.group(1) : Cats
matchObj.group(2) : smarter
</pre>
<h2>The <i>search</i> Function</h2>
<p>This function searches for first occurrence of RE <i>pattern</i> within <i>string</i> with optional <i>flags</i>.</p>
<p>Here is the syntax for this function &minus;</p>
<pre class="result notranslate">
re.search(pattern, string, flags=0)
</pre>
<p>Here is the description of the parameters &minus;</p>
<table class="table table-bordered">
<tr>
<th style="text-align:center;width:10%">Sr.No.</th>
<th style="text-align:center;">Parameter &amp; Description</th>
</tr>
<tr>
<td class="ts">1</td>
<td><p><b>pattern</b></p>
<p>This is the regular expression to be matched.</p></td>
</tr>
<tr>
<td class="ts">2</td>
<td><p><b>string</b></p>
<p>This is the string, which would be searched to match the pattern anywhere in the string.</p></td>
</tr>
<tr>
<td class="ts">3</td>
<td><p><b>flags</b></p>
<p>You can specify different flags using bitwise OR (|). These are modifiers, which are listed in the table below.</p></td>
</tr>
</table>
<p>The <i>re.search</i> function returns a <b>match</b> object on success, <b>none</b> on failure. We use <i>group(num) </i> or <i>groups() </i> function of <b>match</b> object to get matched expression.</p>
<table class="table table-bordered">
<tr>
<th style="text-align:center;width:10%">Sr.No.</th>
<th style="text-align:center;">Match Object Methods &amp; Description</th>
</tr>
<tr>
<td class="ts">1</td>
<td><p><b>group(num=0)</b></p>
<p>This method returns entire match (or specific subgroup num)</p></td>
</tr>
<tr>
<td class="ts">2</td>
<td><p><b>groups()</b></p>
<p>This method returns all matching subgroups in a tuple (empty if there weren't any)</p></td>
</tr>
</table>
<h3>Example</h3>
<div class="demo-view">
<a href="http://tpcg.io/WE021A" target="_blank" rel="nofollow" class="demo"><i class="fa-external-link"></i> Live Demo</a>
</div>
<pre class="prettyprint notranslate">
#!/usr/bin/python
import re
line = "Cats are smarter than dogs";
searchObj = re.search( r'(.*) are (.*?) .*', line, re.M|re.I)
if searchObj:
print "searchObj.group() : ", searchObj.group()
print "searchObj.group(1) : ", searchObj.group(1)
print "searchObj.group(2) : ", searchObj.group(2)
else:
print "Nothing found!!"
</pre>
<p>When the above code is executed, it produces following result &minus;</p>
<pre class="result notranslate">
searchObj.group() : Cats are smarter than dogs
searchObj.group(1) : Cats
searchObj.group(2) : smarter
</pre>
<h2>Matching Versus Searching</h2>
<p>Python offers two different primitive operations based on regular expressions: <b>match</b> checks for a match only at the beginning of the string, while <b>search</b> checks for a match anywhere in the string (this is what Perl does by default).</p>
<h3>Example</h3>
<div class="demo-view">
<a href="http://tpcg.io/1VpZ8k" target="_blank" rel="nofollow" class="demo"><i class="fa-external-link"></i> Live Demo</a>
</div>
<pre class="prettyprint notranslate">
#!/usr/bin/python
import re
line = "Cats are smarter than dogs";
matchObj = re.match( r'dogs', line, re.M|re.I)
if matchObj:
print "match --&gt; matchObj.group() : ", matchObj.group()
else:
print "No match!!"
searchObj = re.search( r'dogs', line, re.M|re.I)
if searchObj:
print "search --&gt; searchObj.group() : ", searchObj.group()
else:
print "Nothing found!!"
</pre>
<p>When the above code is executed, it produces the following result &minus;</p>
<pre class="result notranslate">
No match!!
search --&gt; searchObj.group() : dogs
</pre>
<h2>Search and Replace</h2>
<p>One of the most important <b>re</b> methods that use regular expressions is <b>sub</b>.</p>
<h3>Syntax</h3>
<pre class="result notranslate">
re.sub(pattern, repl, string, max=0)
</pre>
<p>This method replaces all occurrences of the RE <i>pattern</i> in <i>string</i> with <i>repl</i>, substituting all occurrences unless <i>max</i> provided. This method returns modified string.</p>
<h3>Example</h3>
<div class="demo-view">
<a href="http://tpcg.io/jVaoEQ" target="_blank" rel="nofollow" class="demo"><i class="fa-external-link"></i> Live Demo</a>
</div>
<pre class="prettyprint notranslate">
#!/usr/bin/python
import re
phone = "2004-959-559 # This is Phone Number"
# Delete Python-style comments
num = re.sub(r'#.*$', "", phone)
print "Phone Num : ", num
# Remove anything other than digits
num = re.sub(r'\D', "", phone)
print "Phone Num : ", num
</pre>
<p>When the above code is executed, it produces the following result &minus;</p>
<pre class="result notranslate">
Phone Num : 2004-959-559
Phone Num : 2004959559
</pre>
<h2>Regular Expression Modifiers: Option Flags</h2>
<p>Regular expression literals may include an optional modifier to control various aspects of matching. The modifiers are specified as an optional flag. You can provide multiple modifiers using exclusive OR (|), as shown previously and may be represented by one of these &minus;</p>
<table class="table table-bordered">
<tr>
<th style="text-align:center;width:10%">Sr.No.</th>
<th style="text-align:center;">Modifier &amp; Description</th>
</tr>
<tr>
<td class="ts">1</td>
<td><p><b>re.I</b></p>
<p>Performs case-insensitive matching.</p></td>
</tr>
<tr>
<td class="ts">2</td>
<td><p><b>re.L</b></p>
<p>Interprets words according to the current locale. This interpretation affects the alphabetic group (\w and \W), as well as word boundary behavior(\b and \B).</p></td>
</tr>
<tr>
<td class="ts">3</td>
<td><p><b>re.M</b></p>
<p>Makes $ match the end of a line (not just the end of the string) and makes ^ match the start of any line (not just the start of the string).</p></td>
</tr>
<tr>
<td class="ts">4</td>
<td><p><b>re.S</b></p>
<p>Makes a period (dot) match any character, including a newline.</p></td>
</tr>
<tr>
<td class="ts">5</td>
<td><p><b>re.U</b></p>
<p>Interprets letters according to the Unicode character set. This flag affects the behavior of \w, \W, \b, \B.</p></td>
</tr>
<tr>
<td class="ts">6</td>
<td><p><b>re.X</b></p>
<p>Permits "cuter" regular expression syntax. It ignores whitespace (except inside a set [] or when escaped by a backslash) and treats unescaped # as a comment marker.</p></td>
</tr>
</table>
<h2>Regular Expression Patterns</h2>
<p>Except for control characters, <b>(+ ? . * ^ $ ( ) [ ] { } | \)</b>, all characters match themselves. You can escape a control character by preceding it with a backslash.</p>
<p>Following table lists the regular expression syntax that is available in Python &minus;</p>
<table class="table table-bordered">
<tr>
<th style="text-align:center;width:10%">Sr.No.</th>
<th style="text-align:center;">Pattern &amp; Description</th>
</tr>
<tr>
<td class="ts">1</td>
<td><p><b>^</b></p>
<p>Matches beginning of line.</p></td>
</tr>
<tr>
<td class="ts">2</td>
<td><p><b>$</b></p>
<p>Matches end of line.</p></td>
</tr>
<tr>
<td class="ts">3</td>
<td><p><b>.</b></p>
<p>Matches any single character except newline. Using m option allows it to match newline as well.</p></td>
</tr>
<tr>
<td class="ts">4</td>
<td><p><b>[...]</b></p>
<p>Matches any single character in brackets.</p></td>
</tr>
<tr>
<td class="ts">5</td>
<td><p><b>[^...]</b></p>
<p>Matches any single character not in brackets</p></td>
</tr>
<tr>
<td class="ts">6</td>
<td><p><b>re*</b></p>
<p>Matches 0 or more occurrences of preceding expression.</p></td>
</tr>
<tr>
<td class="ts">7</td>
<td><p><b>re+</b></p>
<p>Matches 1 or more occurrence of preceding expression.</p></td>
</tr>
<tr>
<td class="ts">8</td>
<td><p><b>re?</b></p>
<p>Matches 0 or 1 occurrence of preceding expression.</p></td>
</tr>
<tr>
<td class="ts">9</td>
<td><p><b>re{ n}</b></p>
<p>Matches exactly n number of occurrences of preceding expression.</p></td>
</tr>
<tr>
<td class="ts">10</td>
<td><p><b>re{ n,}</b></p>
<p>Matches n or more occurrences of preceding expression.</p></td>
</tr>
<tr>
<td class="ts">11</td>
<td><p><b>re{ n, m}</b></p>
<p>Matches at least n and at most m occurrences of preceding expression.</p></td>
</tr>
<tr>
<td class="ts">12</td>
<td><p><b>a| b</b></p>
<p>Matches either a or b.</p></td>
</tr>
<tr>
<td class="ts">13</td>
<td><p><b>(re)</b></p>
<p>Groups regular expressions and remembers matched text.</p></td>
</tr>
<tr>
<td class="ts">14</td>
<td><p><b>(?imx)</b></p>
<p>Temporarily toggles on i, m, or x options within a regular expression. If in parentheses, only that area is affected.</p></td>
</tr>
<tr>
<td class="ts">15</td>
<td><p><b>(?-imx)</b></p>
<p>Temporarily toggles off i, m, or x options within a regular expression. If in parentheses, only that area is affected.</p></td>
</tr>
<tr>
<td class="ts">16</td>
<td><p><b>(?: re)</b></p>
<p>Groups regular expressions without remembering matched text.</p></td>
</tr>
<tr>
<td class="ts">17</td>
<td><p><b>(?imx: re)</b></p>
<p>Temporarily toggles on i, m, or x options within parentheses.</p></td>
</tr>
<tr>
<td class="ts">18</td>
<td><p><b>(?-imx: re)</b></p>
<p>Temporarily toggles off i, m, or x options within parentheses.</p></td>
</tr>
<tr>
<td class="ts">19</td>
<td><p><b>(?#...)</b></p>
<p>Comment.</p></td>
</tr>
<tr>
<td class="ts">20</td>
<td><p><b>(?= re)</b></p>
<p>Specifies position using a pattern. Doesn't have a range.</p></td>
</tr>
<tr>
<td class="ts">21</td>
<td><p><b>(?! re)</b></p>
<p>Specifies position using pattern negation. Doesn't have a range.</p></td>
</tr>
<tr>
<td class="ts">22</td>
<td><p><b>(?&gt; re)</b></p>
<p>Matches independent pattern without backtracking.</p></td>
</tr>
<tr>
<td class="ts">23</td>
<td><p><b>\w</b></p>
<p>Matches word characters.</p></td>
</tr>
<tr>
<td class="ts">24</td>
<td><p><b>\W</b></p>
<p>Matches nonword characters.</p></td>
</tr>
<tr>
<td class="ts">25</td>
<td><p><b>\s</b></p>
<p>Matches whitespace. Equivalent to [\t\n\r\f].</p></td>
</tr>
<tr>
<td class="ts">26</td>
<td><p><b>\S</b></p>
<p>Matches nonwhitespace.</p></td>
</tr>
<tr>
<td class="ts">27</td>
<td><p><b>\d</b></p>
<p>Matches digits. Equivalent to [0-9].</p></td>
</tr>
<tr>
<td class="ts">28</td>
<td><p><b>\D</b></p>
<p>Matches nondigits.</p></td>
</tr>
<tr>
<td class="ts">29</td>
<td><p><b>\A</b></p>
<p>Matches beginning of string.</p></td>
</tr>
<tr>
<td class="ts">30</td>
<td><p><b>\Z</b></p>
<p>Matches end of string. If a newline exists, it matches just before newline.</p></td>
</tr>
<tr>
<td class="ts">31</td>
<td><p><b>\z</b></p>
<p>Matches end of string.</p></td>
</tr>
<tr>
<td class="ts">32</td>
<td><p><b>\G</b></p>
<p>Matches point where last match finished.</p></td>
</tr>
<tr>
<td class="ts">33</td>
<td><p><b>\b</b></p>
<p>Matches word boundaries when outside brackets. Matches backspace (0x08) when inside brackets.</p></td>
</tr>
<tr>
<td class="ts">34</td>
<td><p><b>\B</b></p>
<p>Matches nonword boundaries.</p></td>
</tr>
<tr>
<td class="ts">35</td>
<td><p><b>\n, \t, etc.</b></p>
<p>Matches newlines, carriage returns, tabs, etc.</p></td>
</tr>
<tr>
<td class="ts">36</td>
<td><p><b>\1...\9</b></p>
<p>Matches nth grouped subexpression.</p></td>
</tr>
<tr>
<td class="ts">37</td>
<td><p><b>\10</b></p>
<p>Matches nth grouped subexpression if it matched already. Otherwise refers to the octal representation of a character code.</p></td>
</tr>
</table>
<h2>Regular Expression Examples</h2>
<h3>Literal characters</h3>
<table class="table table-bordered">
<tr>
<th style="text-align:center;width:10%">Sr.No.</th>
<th style="text-align:center;">Example &amp; Description</th>
</tr>
<tr>
<td class="ts">1</td>
<td><p><b>python</b></p>
<p>Match "python".</p></td>
</tr>
</table>
<h2>Character classes</h2>
<table class="table table-bordered">
<tr>
<th style="text-align:center;width:10%">Sr.No.</th>
<th style="text-align:center;">Example &amp; Description</th>
</tr>
<tr>
<td class="ts">1</td>
<td><p><b>[Pp]ython</b></p>
<p>Match "Python" or "python"</p></td>
</tr>
<tr>
<td class="ts">2</td>
<td><p><b>rub[ye]</b></p>
<p>Match "ruby" or "rube"</p></td>
</tr>
<tr>
<td class="ts">3</td>
<td><p><b>[aeiou]</b></p>
<p>Match any one lowercase vowel</p></td>
</tr>
<tr>
<td class="ts">4</td>
<td><p><b>[0-9]</b></p>
<p>Match any digit; same as [0123456789]</p></td>
</tr>
<tr>
<td class="ts">5</td>
<td><p><b>[a-z]</b></p>
<p>Match any lowercase ASCII letter</p></td>
</tr>
<tr>
<td class="ts">6</td>
<td><p><b>[A-Z]</b></p>
<p>Match any uppercase ASCII letter</p></td>
</tr>
<tr>
<td class="ts">7</td>
<td><p><b>[a-zA-Z0-9]</b></p>
<p>Match any of the above</p></td>
</tr>
<tr>
<td class="ts">8</td>
<td><p><b>[^aeiou]</b></p>
<p>Match anything other than a lowercase vowel</p></td>
</tr>
<tr>
<td class="ts">9</td>
<td><p><b>[^0-9]</b></p>
<p>Match anything other than a digit</p></td>
</tr>
</table>
<h2>Special Character Classes</h2>
<table class="table table-bordered">
<tr>
<th style="text-align:center;width:10%">Sr.No.</th>
<th style="text-align:center;">Example &amp; Description</th>
</tr>
<tr>
<td class="ts">1</td>
<td><p><b>.</b></p>
<p>Match any character except newline</p></td>
</tr>
<tr>
<td class="ts">2</td>
<td><p><b>\d</b></p>
<p>Match a digit: [0-9]</p></td>
</tr>
<tr>
<td class="ts">3</td>
<td><p><b>\D</b></p>
<p>Match a nondigit: [^0-9]</p></td>
</tr>
<tr>
<td class="ts">4</td>
<td><p><b>\s</b></p>
<p>Match a whitespace character: [ \t\r\n\f]</p></td>
</tr>
<tr>
<td class="ts">5</td>
<td><p><b>\S</b></p>
<p>Match nonwhitespace: [^ \t\r\n\f]</p></td>
</tr>
<tr>
<td class="ts">6</td>
<td><p><b>\w</b></p>
<p>Match a single word character: [A-Za-z0-9_]</p></td>
</tr>
<tr>
<td class="ts">7</td>
<td><p><b>\W</b></p>
<p>Match a nonword character: [^A-Za-z0-9_]</p></td>
</tr>
</table>
<h2>Repetition Cases</h2>
<table class="table table-bordered">
<tr>
<th style="text-align:center;width:10%">Sr.No.</th>
<th style="text-align:center;">Example &amp; Description</th>
</tr>
<tr>
<td class="ts">1</td>
<td><p><b>ruby?</b></p>
<p>Match "rub" or "ruby": the y is optional</p></td>
</tr>
<tr>
<td class="ts">2</td>
<td><p><b>ruby*</b></p>
<p>Match "rub" plus 0 or more ys</p></td>
</tr>
<tr>
<td class="ts">3</td>
<td><p><b>ruby+</b></p>
<p>Match "rub" plus 1 or more ys</p></td>
</tr>
<tr>
<td class="ts">4</td>
<td><p><b>\d{3}</b></p>
<p>Match exactly 3 digits</p></td>
</tr>
<tr>
<td class="ts">5</td>
<td><p><b>\d{3,}</b></p>
<p>Match 3 or more digits</p></td>
</tr>
<tr>
<td class="ts">6</td>
<td><p><b>\d{3,5}</b></p>
<p>Match 3, 4, or 5 digits</p></td>
</tr>
</table>
<h2>Nongreedy repetition</h2>
<p>This matches the smallest number of repetitions &minus;</p>
<table class="table table-bordered">
<tr>
<th style="text-align:center;">Sr.No.</th>
<th style="text-align:center;">Example &amp; Description</th>
</tr>
<tr>
<td class="ts">1</td>
<td><p><b>&lt;.*&gt;</b></p>
<p>Greedy repetition: matches "&lt;python&gt;perl&gt;"</p></td>
</tr>
<tr>
<td class="ts">2</td>
<td><p><b>&lt;.*?&gt;</b></p>
<p>Nongreedy: matches "&lt;python&gt;" in "&lt;python&gt;perl&gt;"</p></td>
</tr>
</table>
<h2>Grouping with Parentheses</h2>
<table class="table table-bordered">
<tr>
<th style="text-align:center;width:10%">Sr.No.</th>
<th style="text-align:center;">Example &amp; Description</th>
</tr>
<tr>
<td class="ts">1</td>
<td><p><b>\D\d+</b></p>
<p>No group: + repeats \d</p></td>
</tr>
<tr>
<td class="ts">2</td>
<td><p><b>(\D\d)+</b></p>
<p>Grouped: + repeats \D\d pair</p></td>
</tr>
<tr>
<td class="ts">3</td>
<td><p><b>([Pp]ython(, )?)+</b></p>
<p>Match "Python", "Python, python, python", etc.</p></td>
</tr>
</table>
<h2>Backreferences</h2>
<p>This matches a previously matched group again &minus;</p>
<table class="table table-bordered">
<tr>
<th style="text-align:center;">Sr.No.</th>
<th style="text-align:center;">Example &amp; Description</th>
</tr>
<tr>
<td class="ts">1</td>
<td><p><b>([Pp])ython&amp;\1ails</b></p>
<p>Match python&amp;pails or Python&amp;Pails</p></td>
</tr>
<tr>
<td class="ts">2</td>
<td><p><b>(['"])[^\1]*\1</b></p>
<p>Single or double-quoted string. \1 matches whatever the 1st group matched. \2 matches whatever the 2nd group matched, etc.</p></td>
</tr>
</table>
<h2>Alternatives</h2>
<table class="table table-bordered">
<tr>
<th style="text-align:center;width:10%">Sr.No.</th>
<th style="text-align:center;">Example &amp; Description</th>
</tr>
<tr>
<td class="ts">1</td>
<td><p><b>python|perl</b></p>
<p>Match "python" or "perl"</p></td>
</tr>
<tr>
<td class="ts">2</td>
<td><p><b>rub(y|le))</b></p>
<p>Match "ruby" or "ruble"</p></td>
</tr>
<tr>
<td class="ts">3</td>
<td><p><b>Python(!+|\?)</b></p>
<p>"Python" followed by one or more ! or one ?</p></td>
</tr>
</table>
<h2>Anchors</h2>
<p>This needs to specify match position.</p>
<table class="table table-bordered">
<tr>
<th style="text-align:center;width:10%">Sr.No.</th>
<th style="text-align:center;">Example &amp; Description</th>
</tr>
<tr>
<td class="ts">1</td>
<td><p><b>^Python</b></p>
<p>Match "Python" at the start of a string or internal line</p></td>
</tr>
<tr>
<td class="ts">2</td>
<td><p><b>Python$</b></p>
<p>Match "Python" at the end of a string or line</p></td>
</tr>
<tr>
<td class="ts">3</td>
<td><p><b>\APython</b></p>
<p>Match "Python" at the start of a string</p></td>
</tr>
<tr>
<td class="ts">4</td>
<td><p><b>Python\Z</b></p>
<p>Match "Python" at the end of a string</p></td>
</tr>
<tr>
<td class="ts">5</td>
<td><p><b>\bPython\b</b></p>
<p>Match "Python" at a word boundary</p></td>
</tr>
<tr>
<td class="ts">6</td>
<td><p><b>\brub\B</b></p>
<p>\B is nonword boundary: match "rub" in "rube" and "ruby" but not alone</p></td>
</tr>
<tr>
<td class="ts">7</td>
<td><p><b>Python(?=!)</b></p>
<p>Match "Python", if followed by an exclamation point.</p></td>
</tr>
<tr>
<td class="ts">8</td>
<td><p><b>Python(?!!)</b></p>
<p>Match "Python", if not followed by an exclamation point.</p></td>
</tr>
</table>
<h2>Special Syntax with Parentheses</h2>
<table class="table table-bordered">
<tr>
<th style="text-align:center;width:10%">Sr.No.</th>
<th style="text-align:center;">Example &amp; Description</th>
</tr>
<tr>
<td class="ts">1</td>
<td><p><b>R(?#comment)</b></p>
<p>Matches "R". All the rest is a comment</p></td>
</tr>
<tr>
<td class="ts">2</td>
<td><p><b>R(?i)uby</b></p>
<p>Case-insensitive while matching "uby"</p></td>
</tr>
<tr>
<td class="ts">3</td>
<td><p><b>R(?i:uby)</b></p>
<p>Same as above</p></td>
</tr>
<tr>
<td class="ts">4</td>
<td><p><b>rub(?:y|le))</b></p>
<p>Group only without creating \1 backreference</p></td>
</tr>
</table>
<div class="mui-container-fluid button-borders show" id="bottom_navigation">
<div class="pre-btn">
<a href="/python/python_classes_objects.htm"><i class="fal fa-chevron-circle-left"></i> Previous Page</a>
<a class="hide-on-mobile" href="javascript:printPage();"><i class="fa fa-print"></i> Print Page</a>
</div>
<div class="nxt-btn">
<a href="/python/python_cgi_programming.htm">Next Page <i class="fal fa-chevron-circle-right"></i>&nbsp;</a>
</div>
</div>
<div class="space-bottom" style="margin-bottom:20px;"></div>
<div class="google-bottom-ads" id="google-bottom-ads">
<div>Advertisements</div>
<div>
<div id="adp_bottom_ads"></div>
<script>
var adPos = document.getElementById('adp_bottom_ads');
var mobile_ad_id = "a8a632a6-046e-4f5c-b25f-8bc82ac89fb2";
var desktop_ad_id = "e7ddd6f7-1ab0-494a-bade-887daa4373a0";
var adpushup = adpushup || {};
adpushup.que = adpushup.que || [];
adpushup.que.push(function() {
var ad_id = window.innerWidth <= 768 ? mobile_ad_id : desktop_ad_id;
adPos.innerHTML = '<div id="' + ad_id + '" class="_ap_apex_ad"></div>';
adpushup.triggerAd(ad_id);
});
</script>
</div>
<div class="space-bottom"></div>
</div>
</div>
<!-- Tutorial Content Ends Here -->
<!-- Right Column Starts Here -->
<div class="mui-col-md-2 google-right-ads" id="chapter-menu" style="display: none;">
<input type="hidden" id="vu" name="vu" value=""/>
<div class="space-top"></div>
<div class="mui-panel">
<ul class="paid-menu" id="div-right-menu">
<li><a class="hide-on-mobile" href="javascript:printPage();" title="Print this page"><i class="fa fa-print"></i> Print</a></li>
<li><a href="javascript:void(0)" class="add_notes" title="Add Custom Notes" onclick="add_notes();"><i class="fa fa-edit"></i>&nbsp;Add Notes</a></li>
<li><a href="javascript:void(0)" class="clsPage text-bookmark" id="text-bookmark" title="Add Bookmark" onclick="text_bookmark()"><i class="fal fa-bookmark"></i>&nbsp;Bookmark this page </a></li>
<li><a href="javascript:void(0);" id="report_error_popup" onclick="report_error_popup();" title="Report Error"><i class="fal fa-bell"></i>&nbsp;Report Error</a></li>
<li><a href="javascript:void(0);" id="suggestion_popup" title="Give Suggestions" onclick="suggestion_popup();"><i class="fal fa-badge-check"></i>&nbsp;Suggestions</a></li>
</ul>
<div class="profile-my-notes" id="text_notes_div" style="display:none;">
<textarea rows="10" class="text_notes" id="text_notes" name="text_notes" placeholder="Enter your custom notes..."></textarea>
<br>
<a href="javascript:void(0);" id="submit_text_notes" class="butn-save" onclick="submit_text_notes();"> Save</a>
<a href="javascript:void(0);" class="pop-no butn-cancel" onclick="notesClose();"> Close</a>
<p class="notes_msg" ></p>
</div>
<div class="bottom-menu">
<button class="tp-btn"><i class="fal fa-box-up"></i> <a href="/videotutorials/dashboard.php">Dashboard</a></button>
<button class="tp-btn"><i class="fal fa-sign-out-alt"></i> <a href="/videotutorials/userlogout.php">Logout</a></button>
</div>
</div>
</div>
<div class="mui-col-md-2 google-right-ads" id="google-right-ads">
<div class="space-top"></div>
<div class="google-right-ad" style="margin: 0px auto !important;margin-top:5px;min-height:250px!important">
<div id="f0f6cff6-393d-4170-b63a-d9ff06ca9f25" class="_ap_apex_ad">
<script>
var adpushup = adpushup || {};
adpushup.que = adpushup.que || [];
adpushup.que.push(function() {
adpushup.triggerAd("f0f6cff6-393d-4170-b63a-d9ff06ca9f25");
});
</script>
</div>
</div>
<div class="google-right-ad" style="margin-top:16px;min-height:250px!important">
<div id="5c0affd5-724a-475c-bfcd-cecbc7660c36" class="_ap_apex_ad">
<script>
var adpushup = adpushup || {};
adpushup.que = adpushup.que || [];
adpushup.que.push(function() {
adpushup.triggerAd("5c0affd5-724a-475c-bfcd-cecbc7660c36");
});
</script>
</div>
</div>
<div class="google-right-ad" style="margin-top:16px;margin-bottom:15px;min-height:600px!important">
<div id="8f8814c2-d64c-4d26-9cb9-543653c0b48f" class="_ap_apex_ad">
<script>
var adpushup = adpushup || {};
adpushup.que = adpushup.que || [];
adpushup.que.push(function() {
adpushup.triggerAd("8f8814c2-d64c-4d26-9cb9-543653c0b48f");
});
</script>
</div>
</div>
<div class="space-bottom" style="margin-bottom:15px;"></div>
</div>
<!-- Right Column Ends Here -->
</div>
</div>
<div class="clear"></div>
<footer id="footer">
<div class="mui--text-center">
<div class="mui--text-caption mui--text-light">
<a href="/index.htm" class="logo"><img class="img-responsive" src="/images/logo-black.png" alt="Tutorials Point" title="Tutorials Point"></a>
</div>
<ul class="mui-list--inline mui--text-body2 mui--text-light">
<li><a href="/about/index.htm"><i class="fal fa-globe"></i> About us</a></li>
<li><a href="/about/return_refund_policy.htm"> <i class="fal fa-asterisk"></i> Refund Policy</a></li>
<li><a href="/about/about_terms_of_use.htm"><i class="fal fa-asterisk"></i> Terms of use</a></li>
<li><a href="/about/about_privacy.htm"> <i class="fal fa-shield-check"></i> Privacy Policy</a></li>
<li><a href="/about/faq.htm"><i class="fal fa-question-circle"></i> FAQ's</a></li>
<li><a href="/about/contact_us.htm"><i class="fal fa-map-marker-alt"></i> Contact</a></li>
</ul>
<div class="mui--text-caption mui--text-light bottom-copyright-text">&copy; Copyright 2021. All Rights Reserved.</div>
</div>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-232293-6"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-232293-6');
</script>
<script src="/themes/js/jquery.colorbox-min.js"></script>
<script>
var tryit = new $.TryIt('/python/try_python.php');
tryit.compile();
</script><script src="https://www.tutorialspoint.com/videotutorials/js/text_menu.js?v=1"></script>
<script>
$(document).ready(function (){
var cval = readCookie('subscription_purchased');
if(cval == '1'){
// logged in and purchased subscription.
$('#google-right-ads').remove();
$('#adp1').remove();
$('#adp2').remove();
$('#stickyparent').remove();
$('#google-top-ads').remove();
$('#adp_top_ads').remove();
$('#google-bottom-ads').remove();
$('.tutorial-content hr').remove();
$('.login').remove();
$('#chapter-menu').show();
$('.content h1').css("border-bottom", "none");
$('.google-right-ads').css("padding-right", "0px");
$('.tutorial-content').css("border-radius", "10px");
$('#chapter-menu .mui-panel').css("border-radius", "10px");
}
});
</script>
</footer>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201