{"id":1875,"date":"2020-12-27T02:56:17","date_gmt":"2020-12-27T02:56:17","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1875"},"modified":"2020-12-28T01:05:38","modified_gmt":"2020-12-28T01:05:38","slug":"python-string-startswith","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-string-methods\/python-string-startswith\/","title":{"rendered":"Python String startswith()"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Python string <code>startswith()<\/code> method to check if a string begins with another string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-string-startswith-method'>Introduction to the Python string startswith() method <a href=\"#introduction-to-the-python-string-startswith-method\" class=\"anchor\" id=\"introduction-to-the-python-string-startswith-method\" title=\"Anchor for Introduction to the Python string startswith() method\">#<\/a><\/h2>\n\n\n\n<p>The <code>startswith()<\/code> method returns <code>True<\/code> if a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-string\/\">string<\/a> starts with another string. Otherwise, it returns <code>False<\/code>.<\/p>\n\n\n\n<p>The following shows the syntax of the <code>startswith()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">str.startswith(prefix, &#91;,start &#91;,end ])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>startswith()<\/code> method accepts three parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>prefix<\/code> is a string or a tuple of strings to search for. The <code>prefix<\/code> parameter is mandatory. <\/li><li><code>start<\/code> is the position that the method starts looking for the <code>prefix<\/code>. The <code>start<\/code> parameter is optional.<\/li><li><code>end<\/code> is the position in the string that the method stops searching for the <code>prefix<\/code>. The <code>end<\/code> parameter is also optional.<\/li><\/ul>\n\n\n\n<p>Note that the <code>startswith()<\/code> method is case-sensitive. In other words, it will look for the <code>prefix<\/code> case-sensitively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-string-startswith-method-examples'>Python string startswith() method examples <a href=\"#python-string-startswith-method-examples\" class=\"anchor\" id=\"python-string-startswith-method-examples\" title=\"Anchor for Python string startswith() method examples\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s take some examples of using the string <code>startswith()<\/code> method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='1-using-the-startswith-method-to-check-if-a-string-begins-with-another-string'>1) Using the startswith() method to check if a string begins with another string <a href=\"#1-using-the-startswith-method-to-check-if-a-string-begins-with-another-string\" class=\"anchor\" id=\"1-using-the-startswith-method-to-check-if-a-string-begins-with-another-string\" title=\"Anchor for 1) Using the startswith() method to check if a string begins with another string\">#<\/a><\/h3>\n\n\n\n<p>The following example shows how to use the string <code>startswith()<\/code> method to check if a string starts with another string:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">s = <span class=\"hljs-string\">'Make it work, make it right, make it fast.'<\/span>\nresult = s.startswith(<span class=\"hljs-string\">'Make'<\/span>)\nprint(result)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-literal\">True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>As mentioned earlier, the <code>startswith()<\/code> method searches for a string case-sensitively. Therefore, the following example returns <code>False<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">s = <span class=\"hljs-string\">'Make it work, make it right, make it fast.'<\/span>\nresult = s.startswith(<span class=\"hljs-string\">'make'<\/span>)\nprint(result)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-literal\">False<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='2-using-the-startswith-method-with-a-tuple'>2) Using the startswith() method with a tuple <a href=\"#2-using-the-startswith-method-with-a-tuple\" class=\"anchor\" id=\"2-using-the-startswith-method-with-a-tuple\" title=\"Anchor for 2) Using the startswith() method with a tuple\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>startswith()<\/code> method to check if a string starts with one of the strings in a tuple:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">s\u00a0=\u00a0<span class=\"hljs-string\">'Make\u00a0it\u00a0work,\u00a0make\u00a0it\u00a0right,\u00a0make\u00a0it\u00a0fast.'<\/span>\nresult\u00a0=\u00a0s.startswith((<span class=\"hljs-string\">'Make'<\/span>,<span class=\"hljs-string\">'make'<\/span>))\n<span class=\"hljs-keyword\">print<\/span>(result)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='3-using-the-startswith-method-with-the-start-parameter'>3) Using the startswith() method with the start parameter <a href=\"#3-using-the-startswith-method-with-the-start-parameter\" class=\"anchor\" id=\"3-using-the-startswith-method-with-the-start-parameter\" title=\"Anchor for 3) Using the startswith() method with the start parameter\">#<\/a><\/h3>\n\n\n\n<p>The following example illustrates how to use the <code>startswith()<\/code> method to check if the string starts with the word make in lowercase starting from position 14:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">s = <span class=\"hljs-string\">'Make it work, make it right, make it fast.'<\/span>\nresult = s.startswith(<span class=\"hljs-string\">'make'<\/span>, <span class=\"hljs-number\">14<\/span>)\nprint(result)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-literal\">True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the Python string <code>startswith()<\/code> method to determine if a string begins with another string.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1875\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-string-methods\/python-string-startswith\/\"\n\t\t\t\tdata-post-title=\"Python String startswith()\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1875\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-string-methods\/python-string-startswith\/\"\n\t\t\t\tdata-post-title=\"Python String startswith()\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the Python string startswith() method to check if a string begins with another string.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1925,"menu_order":5,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1875","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1875","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=1875"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1875\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1925"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=1875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}