{"id":23402,"date":"2022-11-25T20:00:54","date_gmt":"2022-11-25T14:30:54","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-using-as-a-stand-in-for-a-single-letter\/"},"modified":"2022-11-25T20:00:54","modified_gmt":"2022-11-25T14:30:54","slug":"solved-using-as-a-stand-in-for-a-single-letter","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-using-as-a-stand-in-for-a-single-letter\/","title":{"rendered":"[Solved] using &#8220;?&#8221; as a stand-in for a single letter"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-26198050\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"26198050\" data-parentid=\"26197950\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>If you want to avoid regular expressions altogether and need to support only that single case illustrated in your post, then this should work:<\/p>\n<pre><code>def match(string, pattern):\n    if len(string) != len(pattern):\n        return False\n    for s, p in zip(string, pattern):\n        if s != p and p != '?':\n            return False\n    return True\n<\/code><\/pre>\n<p><strong>Execution example:<\/strong><\/p>\n<pre><code>&gt;&gt;&gt; match('ABC', 'ABC')\nTrue\n&gt;&gt;&gt; match('ABC', 'AbC')\nFalse\n&gt;&gt;&gt; match('ABC', 'A?C')\nTrue\n<\/code><\/pre>\n<p><strong>Explanation:<\/strong><\/p>\n<ol>\n<li>\n<p>You may iterate over a string, with each iteration yielding another character:<\/p>\n<pre><code>&gt;&gt;&gt; [i for i in 'ABC']\n['A', 'B', 'C']\n<\/code><\/pre>\n<\/li>\n<li>\n<p><code>zip<\/code> allows you to iterate over both lists together:<\/p>\n<pre><code>&gt;&gt;&gt; [(s, p) for (s, p) in zip('STRING1', 'string2')]\n[('S', 's'), ('T', 't'), ('R', 'r'), ('I', 'i'), ('N', 'n'), ('G', 'g'), ('1', '2')]\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<hr>\n<p>A few remarks regarding your code snippet:<\/p>\n<ol>\n<li>There is no need to convert the input strings to <code>str<\/code> since they are guaranteed to be of that type.<\/li>\n<li>Check for <code>True<\/code> and <code>False<\/code> values using the <code>is<\/code> operator. As @PadraicCunningham commented, just checking <code>if m<\/code> (without <code>== True<\/code> or <code>is True<\/code>) would probably suffice in this case, since the value returned is certainly either <code>True<\/code> or <code>False<\/code>, but I prefer being explicit nonetheless. See this for more details.<\/li>\n<li>Your <code>while<\/code> loop either runs forever or never. Your test of <code>pattern &lt;= string<\/code> does not change throughout the loop and compares them lexicographically.<\/li>\n<li>When using slicing to access just part of an iterable, there is no need to explicitly mention it&#8217;s beginning if it starts from 0, e.g. <code>string[0:len(pattern)]<\/code> is equivalent to <code>string[:len(pattern)]<\/code>.<\/li>\n<li>You have incremented the <code>index<\/code> variable without initializing it first, and you don&#8217;t use it anywhere else.<\/li>\n<li>\n<p>The previous comment is no longer relevant, since you&#8217;ve updated your code, but your new statement doesn&#8217;t make any sense either. <code>Python<\/code> doesn&#8217;t have built-in support for adding an <code>int<\/code> to a <code>str<\/code>, since it&#8217;s not well-defined. Executing that line of code will throw the following exception:<\/p>\n<pre><code>&gt;&gt;&gt; 'ABC'+1\nTraceback (most recent call last):\n  File \"&lt;stdin&gt;\", line 1, in &lt;module&gt;\nTypeError: cannot concatenate 'str' and 'int' objects\n<\/code><\/pre>\n<p>Furthermore, even if it would have returned some value, you can&#8217;t just set it into an <code>str<\/code> object since <code>str<\/code> objects are immutable (in the following example -1 denotes the length of the iterable minus 1):<\/p>\n<pre><code>&gt;&gt;&gt; x = 'ABCDE'\n&gt;&gt;&gt; x[1:-1] = '123'\nTraceback (most recent call last):\n  File \"&lt;stdin&gt;\", line 1, in &lt;module&gt;\nTypeError: 'str' object does not support item assignment\n<\/code><\/pre>\n<p>With lists, it would work:<\/p>\n<pre><code>&gt;&gt;&gt; x = ['A', 'B', 'C', 'D', 'E']\n&gt;&gt;&gt; x[1:-1] = [1, 2, 3, 4, 5]\n&gt;&gt;&gt; x\n['A', 1, 2, 3, 4, 5, 'E']\n<\/code><\/pre>\n<\/li>\n<li>\n<p>The previous comment is no longer relevant since you&#8217;ve updated your code again, but that update is flawed as well. Now you&#8217;re comparing an <code>int<\/code> against a <code>str<\/code> in the test clause of your <code>while<\/code> loop. In Python 3, this will always raise an exception (though, not in Python 2):<\/p>\n<pre><code>&gt;&gt;&gt; 'ABC' &gt; 0\nTraceback (most recent call last):\n  File \"&lt;stdin&gt;\", line 1, in &lt;module&gt;\nTypeError: unorderable types: str() &gt; int()\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<hr>\n<h2>EDIT:<\/h2>\n<p>Following your comment on the actual desired behavior, here is an updated solution. In a nutshell, it&#8217;s similar to the previous one, but it uses a modified version of the <code>match<\/code> function that expects both inputs to be of the exact same length. <code>match_all<\/code> extracts such substrings from the original <code>string<\/code> variable and invokes <code>match<\/code> with each of these substrings along with the original <code>pattern<\/code> variable:<\/p>\n<pre><code>def match_all(string, pattern):\n    if len(string) &lt; len(pattern):\n        return False\n    for index in range(0, len(string) - len(pattern) + 1):\n        if match(string[index:index + len(pattern)], pattern) is True:\n            return True\n    return False\n\ndef match(string, pattern):\n    for s, p in zip(string, pattern):\n        if s != p and p != '?':\n            return False\n    return True\n<\/code><\/pre>\n<p><strong>Execution example:<\/strong><\/p>\n<pre><code>&gt;&gt;&gt; match_all('ABC', '????')\nFalse\n&gt;&gt;&gt; match_all('ABC', '???')\nTrue\n&gt;&gt;&gt; match_all('ABC', 'AB')\nTrue\n&gt;&gt;&gt; match_all('ABC', 'BC')\nTrue\n&gt;&gt;&gt; match_all('ABC', 'Ab')\nFalse\n&gt;&gt;&gt; match_all('ABC', 'A?')\nTrue\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">12<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved using &#8220;?&#8221; as a stand-in for a single letter <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] If you want to avoid regular expressions altogether and need to support only that single case illustrated in your post, then this should work: def match(string, pattern): if len(string) != len(pattern): return False for s, p in zip(string, pattern): if s != p and p != &#8216;?&#8217;: return False return True Execution example: &gt;&gt;&gt; &#8230; <a title=\"[Solved] using &#8220;?&#8221; as a stand-in for a single letter\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-using-as-a-stand-in-for-a-single-letter\/\" aria-label=\"More on [Solved] using &#8220;?&#8221; as a stand-in for a single letter\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[2420,349,2991],"class_list":["post-23402","post","type-post","status-publish","format-standard","hentry","category-solved","tag-pattern-matching","tag-python","tag-wildcard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] using &quot;?&quot; as a stand-in for a single letter - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-using-as-a-stand-in-for-a-single-letter\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] using &quot;?&quot; as a stand-in for a single letter - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] If you want to avoid regular expressions altogether and need to support only that single case illustrated in your post, then this should work: def match(string, pattern): if len(string) != len(pattern): return False for s, p in zip(string, pattern): if s != p and p != &#039;?&#039;: return False return True Execution example: &gt;&gt;&gt; ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-using-as-a-stand-in-for-a-single-letter\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-25T14:30:54+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-using-as-a-stand-in-for-a-single-letter\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-using-as-a-stand-in-for-a-single-letter\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] using &#8220;?&#8221; as a stand-in for a single letter\",\"datePublished\":\"2022-11-25T14:30:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-using-as-a-stand-in-for-a-single-letter\\\/\"},\"wordCount\":397,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"pattern-matching\",\"python\",\"wildcard\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-using-as-a-stand-in-for-a-single-letter\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-using-as-a-stand-in-for-a-single-letter\\\/\",\"name\":\"[Solved] using \\\"?\\\" as a stand-in for a single letter - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-11-25T14:30:54+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-using-as-a-stand-in-for-a-single-letter\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-using-as-a-stand-in-for-a-single-letter\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-using-as-a-stand-in-for-a-single-letter\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] using &#8220;?&#8221; as a stand-in for a single letter\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] using \"?\" as a stand-in for a single letter - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-using-as-a-stand-in-for-a-single-letter\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] using \"?\" as a stand-in for a single letter - JassWeb","og_description":"[ad_1] If you want to avoid regular expressions altogether and need to support only that single case illustrated in your post, then this should work: def match(string, pattern): if len(string) != len(pattern): return False for s, p in zip(string, pattern): if s != p and p != '?': return False return True Execution example: &gt;&gt;&gt; ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-using-as-a-stand-in-for-a-single-letter\/","og_site_name":"JassWeb","article_published_time":"2022-11-25T14:30:54+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-using-as-a-stand-in-for-a-single-letter\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-using-as-a-stand-in-for-a-single-letter\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] using &#8220;?&#8221; as a stand-in for a single letter","datePublished":"2022-11-25T14:30:54+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-using-as-a-stand-in-for-a-single-letter\/"},"wordCount":397,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["pattern-matching","python","wildcard"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-using-as-a-stand-in-for-a-single-letter\/","url":"https:\/\/jassweb.com\/solved\/solved-using-as-a-stand-in-for-a-single-letter\/","name":"[Solved] using \"?\" as a stand-in for a single letter - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-25T14:30:54+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-using-as-a-stand-in-for-a-single-letter\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-using-as-a-stand-in-for-a-single-letter\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-using-as-a-stand-in-for-a-single-letter\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] using &#8220;?&#8221; as a stand-in for a single letter"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/23402","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=23402"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/23402\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=23402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=23402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=23402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}