{"id":5571,"date":"2023-09-08T20:33:54","date_gmt":"2023-09-08T15:03:54","guid":{"rendered":"https:\/\/nolowiz.com\/?p=5571"},"modified":"2023-09-08T20:33:56","modified_gmt":"2023-09-08T15:03:56","slug":"python-string-methods","status":"publish","type":"post","link":"https:\/\/nolowiz.com\/python-string-methods\/","title":{"rendered":"Python String Methods"},"content":{"rendered":"\n<p>Python provides a variety of built-in string methods that allow you to manipulate and work with strings. These methods help you perform tasks like finding substrings, replacing text, converting cases, and more. By learning these methods, you can efficiently handle string operations in your <a href=\"https:\/\/www.python.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python <\/a>programs.<\/p>\n\n\n\n<p>Let&#8217;s dive into some of the most commonly used Python string methods:<\/p>\n\n\n\n<h2>1. str.upper() and str.lower()<\/h2>\n\n\n\n<p>The <code>str.upper()<\/code> the method used to convert a string to uppercase and  <code>str.lower()<\/code> method used to convert string to lowercase.<\/p>\n\n\n\n<p>For example : <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntext = &quot;Hello, World!&quot;\nuppercase_text = text.upper()\nlowercase_text = text.lower()\n\nprint(uppercase_text)  \nprint(lowercase_text)  \n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>HELLO, WORLD!\nhello, world!\n<\/code><\/pre>\n\n\n\n<h2>2. str.strip()<\/h2>\n\n\n\n<p>The <code>strip()<\/code> method removes leading and trailing whitespace characters (spaces, tabs, newlines) from a string. It&#8217;s particularly handy when you&#8217;re processing user input or cleaning up data.<\/p>\n\n\n\n<p> For example : <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntext = &quot;   Python String Methods   &quot;\ncleaned_text = text.strip()\n\nprint(cleaned_text) \n\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Python String Methods<\/code><\/pre>\n\n\n\n<p>We can see that it removed leading and trailing spaces.<\/p>\n\n\n\n<h2>3. str.split()<\/h2>\n\n\n\n<p>This method splits a string into a list of substrings based on a specified delimiter. It&#8217;s frequently used for parsing text and working with CSV or TSV files.<\/p>\n\n\n\n<p>For example :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncsv_data = &quot;John,Doe,30&quot;\ndata_list = csv_data.split(',')\n\nprint(data_list)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'John', 'Doe', '30']<\/code><\/pre>\n\n\n\n<h2>4. str.join()<\/h2>\n\n\n\n<p>The join() method is the opposite of split(). The str.join() method is used to concatenate elements of a string iterable into a new string, using the string as a separator.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nwords = &#91;&quot;Python&quot;, &quot;is&quot;, &quot;awesome&quot;]\nsentence = &quot; &quot;.join(words)\n\nprint(sentence) \n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Python is awesome<\/code><\/pre>\n\n\n\n<h2>5. str.replace()<\/h2>\n\n\n\n<p>The str.replace() method is used to replace occurrences of a substring within a string with a new substring. It takes two arguments: the substring to be replaced and the new substring.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntext = &quot;I love programming in JavaScript.&quot;\nmodified_text = text.replace(&quot;JavaScript&quot;, &quot;Python&quot;)\n\nprint(modified_text)  \n\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>I love programming in Python.<\/code><\/pre>\n\n\n\n<h2>6. str.find() and str.index()<\/h2>\n\n\n\n<p>Both methods are used to search for a substring within a string.<\/p>\n\n\n\n<p>The str.find method returns the index of the first occurrence of a substring within a string. If the substring is not found, it returns -1. It takes an optional start and end index to search within.<\/p>\n\n\n\n<p>The str.index() method is used to find the index of the first occurrence of a substring in a string. It raises a ValueError if the substring is not found.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntext = &quot;Python is a powerful language.&quot;\nposition1 = text.find(&quot;is&quot;)  \nposition2 = text.index(&quot;Java&quot;)  # Raises ValueError\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>7<\/code><\/pre>\n\n\n\n<h2>7. str.startswith() and str.endswith()<\/h2>\n\n\n\n<p>The str.startswith() method checks if a string starts with a specified substring. It returns True if the string starts with the substring, and False otherwise. The method takes the substring as an argument and is case-sensitive.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntext = &quot;www.example.com&quot;\nstarts_with_www = text.startswith(&quot;www&quot;)\n<\/pre><\/div>\n\n\n<p>Here &#8220;starts_with_www &#8221; variable holds the <span class=\"has-inline-color has-vivid-red-color\">True<\/span> value.<\/p>\n\n\n\n<p>The str.endswith() method checks if a string ends with a specified suffix. It returns True if the string ends with the suffix, and False otherwise. <\/p>\n\n\n\n<p>For example :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntext = &quot;www.example.com&quot;\nends_with_net = text.endswith(&quot;net&quot;) \n<\/pre><\/div>\n\n\n<p> Here &#8220;ends_with_net&#8221; variable holds the <span class=\"has-inline-color has-vivid-red-color\">False <\/span>value. <\/p>\n\n\n\n<h2>Conclusion<\/h2>\n\n\n\n<p>There you have it different string methods used in Python Programming. <a href=\"https:\/\/nolowiz.com\/python-string-find-substring-check-string-contains-substring\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python String Find Substring \u2013 Check String Contains Substring<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python provides a variety of built-in string methods that allow you to manipulate and work with strings. These methods help you perform tasks like finding substrings, replacing text, converting cases, and more. By learning these methods, you can efficiently handle string operations in your Python programs. Let&#8217;s dive into some of the most commonly used &#8230; <a title=\"Python String Methods\" class=\"read-more\" href=\"https:\/\/nolowiz.com\/python-string-methods\/\" aria-label=\"More on Python String Methods\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":5589,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[25],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python String Methods - NoloWiz<\/title>\n<meta name=\"description\" content=\"Python provides a variety of built-in string methods. In this tutorial, we will learn about different string methods.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/nolowiz.com\/python-string-methods\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python String Methods - NoloWiz\" \/>\n<meta property=\"og:description\" content=\"Python provides a variety of built-in string methods. In this tutorial, we will learn about different string methods.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nolowiz.com\/python-string-methods\/\" \/>\n<meta property=\"og:site_name\" content=\"NoloWiz\" \/>\n<meta property=\"article:published_time\" content=\"2023-09-08T15:03:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-09-08T15:03:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/nolowiz.com\/wp-content\/uploads\/2023\/09\/python-string-methods-feature-image.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rupesh Sreeraman\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/nolowiz.com\/#organization\",\"name\":\"NoloWiz\",\"url\":\"https:\/\/nolowiz.com\/\",\"sameAs\":[],\"logo\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/nolowiz.com\/#logo\",\"inLanguage\":\"en\",\"url\":\"https:\/\/nolowiz.com\/wp-content\/uploads\/2021\/01\/cropped-android-chrome-512x512-2.png\",\"contentUrl\":\"https:\/\/nolowiz.com\/wp-content\/uploads\/2021\/01\/cropped-android-chrome-512x512-2.png\",\"width\":512,\"height\":512,\"caption\":\"NoloWiz\"},\"image\":{\"@id\":\"https:\/\/nolowiz.com\/#logo\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/nolowiz.com\/#website\",\"url\":\"https:\/\/nolowiz.com\/\",\"name\":\"NoloWiz\",\"description\":\"Technology news, tips and tutorials\",\"publisher\":{\"@id\":\"https:\/\/nolowiz.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/nolowiz.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/nolowiz.com\/python-string-methods\/#primaryimage\",\"inLanguage\":\"en\",\"url\":\"https:\/\/nolowiz.com\/wp-content\/uploads\/2023\/09\/python-string-methods-feature-image.jpg\",\"contentUrl\":\"https:\/\/nolowiz.com\/wp-content\/uploads\/2023\/09\/python-string-methods-feature-image.jpg\",\"width\":1200,\"height\":628,\"caption\":\"Python String Methods feature image\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/nolowiz.com\/python-string-methods\/#webpage\",\"url\":\"https:\/\/nolowiz.com\/python-string-methods\/\",\"name\":\"Python String Methods - NoloWiz\",\"isPartOf\":{\"@id\":\"https:\/\/nolowiz.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/nolowiz.com\/python-string-methods\/#primaryimage\"},\"datePublished\":\"2023-09-08T15:03:54+00:00\",\"dateModified\":\"2023-09-08T15:03:56+00:00\",\"description\":\"Python provides a variety of built-in string methods. In this tutorial, we will learn about different string methods.\",\"breadcrumb\":{\"@id\":\"https:\/\/nolowiz.com\/python-string-methods\/#breadcrumb\"},\"inLanguage\":\"en\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/nolowiz.com\/python-string-methods\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/nolowiz.com\/python-string-methods\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/nolowiz.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python String Methods\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/nolowiz.com\/python-string-methods\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/nolowiz.com\/python-string-methods\/#webpage\"},\"author\":{\"@id\":\"https:\/\/nolowiz.com\/#\/schema\/person\/6ef6f57a69193ce0993d74a8b6ac4414\"},\"headline\":\"Python String Methods\",\"datePublished\":\"2023-09-08T15:03:54+00:00\",\"dateModified\":\"2023-09-08T15:03:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/nolowiz.com\/python-string-methods\/#webpage\"},\"wordCount\":427,\"publisher\":{\"@id\":\"https:\/\/nolowiz.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/nolowiz.com\/python-string-methods\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/nolowiz.com\/wp-content\/uploads\/2023\/09\/python-string-methods-feature-image.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/nolowiz.com\/#\/schema\/person\/6ef6f57a69193ce0993d74a8b6ac4414\",\"name\":\"Rupesh Sreeraman\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/nolowiz.com\/#personlogo\",\"inLanguage\":\"en\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0b6c0696ed1695a540102a80daa94ad0?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0b6c0696ed1695a540102a80daa94ad0?s=96&d=mm&r=g\",\"caption\":\"Rupesh Sreeraman\"},\"sameAs\":[\"http:\/\/nolowiz.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python String Methods - NoloWiz","description":"Python provides a variety of built-in string methods. In this tutorial, we will learn about different string methods.","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:\/\/nolowiz.com\/python-string-methods\/","og_locale":"en_US","og_type":"article","og_title":"Python String Methods - NoloWiz","og_description":"Python provides a variety of built-in string methods. In this tutorial, we will learn about different string methods.","og_url":"https:\/\/nolowiz.com\/python-string-methods\/","og_site_name":"NoloWiz","article_published_time":"2023-09-08T15:03:54+00:00","article_modified_time":"2023-09-08T15:03:56+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/nolowiz.com\/wp-content\/uploads\/2023\/09\/python-string-methods-feature-image.jpg","path":"\/home\/rupeshsreeraman\/public_html\/nolowiz.com\/wp-content\/uploads\/2023\/09\/python-string-methods-feature-image.jpg","size":"full","id":5589,"alt":"Python String Methods feature image","pixels":753600,"type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rupesh Sreeraman","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/nolowiz.com\/#organization","name":"NoloWiz","url":"https:\/\/nolowiz.com\/","sameAs":[],"logo":{"@type":"ImageObject","@id":"https:\/\/nolowiz.com\/#logo","inLanguage":"en","url":"https:\/\/nolowiz.com\/wp-content\/uploads\/2021\/01\/cropped-android-chrome-512x512-2.png","contentUrl":"https:\/\/nolowiz.com\/wp-content\/uploads\/2021\/01\/cropped-android-chrome-512x512-2.png","width":512,"height":512,"caption":"NoloWiz"},"image":{"@id":"https:\/\/nolowiz.com\/#logo"}},{"@type":"WebSite","@id":"https:\/\/nolowiz.com\/#website","url":"https:\/\/nolowiz.com\/","name":"NoloWiz","description":"Technology news, tips and tutorials","publisher":{"@id":"https:\/\/nolowiz.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/nolowiz.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en"},{"@type":"ImageObject","@id":"https:\/\/nolowiz.com\/python-string-methods\/#primaryimage","inLanguage":"en","url":"https:\/\/nolowiz.com\/wp-content\/uploads\/2023\/09\/python-string-methods-feature-image.jpg","contentUrl":"https:\/\/nolowiz.com\/wp-content\/uploads\/2023\/09\/python-string-methods-feature-image.jpg","width":1200,"height":628,"caption":"Python String Methods feature image"},{"@type":"WebPage","@id":"https:\/\/nolowiz.com\/python-string-methods\/#webpage","url":"https:\/\/nolowiz.com\/python-string-methods\/","name":"Python String Methods - NoloWiz","isPartOf":{"@id":"https:\/\/nolowiz.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/nolowiz.com\/python-string-methods\/#primaryimage"},"datePublished":"2023-09-08T15:03:54+00:00","dateModified":"2023-09-08T15:03:56+00:00","description":"Python provides a variety of built-in string methods. In this tutorial, we will learn about different string methods.","breadcrumb":{"@id":"https:\/\/nolowiz.com\/python-string-methods\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nolowiz.com\/python-string-methods\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/nolowiz.com\/python-string-methods\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/nolowiz.com\/"},{"@type":"ListItem","position":2,"name":"Python String Methods"}]},{"@type":"Article","@id":"https:\/\/nolowiz.com\/python-string-methods\/#article","isPartOf":{"@id":"https:\/\/nolowiz.com\/python-string-methods\/#webpage"},"author":{"@id":"https:\/\/nolowiz.com\/#\/schema\/person\/6ef6f57a69193ce0993d74a8b6ac4414"},"headline":"Python String Methods","datePublished":"2023-09-08T15:03:54+00:00","dateModified":"2023-09-08T15:03:56+00:00","mainEntityOfPage":{"@id":"https:\/\/nolowiz.com\/python-string-methods\/#webpage"},"wordCount":427,"publisher":{"@id":"https:\/\/nolowiz.com\/#organization"},"image":{"@id":"https:\/\/nolowiz.com\/python-string-methods\/#primaryimage"},"thumbnailUrl":"https:\/\/nolowiz.com\/wp-content\/uploads\/2023\/09\/python-string-methods-feature-image.jpg","articleSection":["Python"],"inLanguage":"en"},{"@type":"Person","@id":"https:\/\/nolowiz.com\/#\/schema\/person\/6ef6f57a69193ce0993d74a8b6ac4414","name":"Rupesh Sreeraman","image":{"@type":"ImageObject","@id":"https:\/\/nolowiz.com\/#personlogo","inLanguage":"en","url":"https:\/\/secure.gravatar.com\/avatar\/0b6c0696ed1695a540102a80daa94ad0?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0b6c0696ed1695a540102a80daa94ad0?s=96&d=mm&r=g","caption":"Rupesh Sreeraman"},"sameAs":["http:\/\/nolowiz.com"]}]}},"_links":{"self":[{"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/posts\/5571"}],"collection":[{"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/comments?post=5571"}],"version-history":[{"count":31,"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/posts\/5571\/revisions"}],"predecessor-version":[{"id":5603,"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/posts\/5571\/revisions\/5603"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/media\/5589"}],"wp:attachment":[{"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/media?parent=5571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/categories?post=5571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/tags?post=5571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}