

{"id":123212,"date":"2023-11-02T12:15:03","date_gmt":"2023-11-02T06:45:03","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=123212"},"modified":"2024-02-28T11:09:11","modified_gmt":"2024-02-28T05:39:11","slug":"python-program-on-methods-of-string-class","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-program-on-methods-of-string-class\/","title":{"rendered":"Python Program on Methods of String Class"},"content":{"rendered":"<p>In this article, we will delve into various methods of the String class in Python, exploring how they can be utilized for effective string manipulation. These methods provide powerful tools for transforming and analyzing strings, showcasing the versatility of Python in handling textual data.<\/p>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>Basic understanding of Python syntax, variables, and input\/output operations.<\/li>\n<li>Familiarity with fundamental concepts such as string formatting, counting, and searching.<\/li>\n<\/ul>\n<h3>Topic Explanation<\/h3>\n<p>The code snippet introduces multiple methods of the String class, each serving a distinct purpose. From altering the case of characters to searching for substrings, counting occurrences, and formatting strings, these methods demonstrate the extensive capabilities of Python in string manipulation. This article will elucidate each method and its application, providing practical insights for both beginners and intermediate Python developers.<\/p>\n<p>Furthermore, the article explores the practical implications of these String class methods. It sheds light on how these techniques are essential for real-world applications, from data processing to text analysis. By delving into these practical aspects, the article aims to provide readers with a concrete understanding of how these methods can be applied in various scenarios. This practical insight is particularly valuable for developers looking to enhance their skills in Python string manipulation for everyday programming challenges.<\/p>\n<h4>Code:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Methods of String class\r\ns = \"     Data-Flair    \"\r\n# Convert the string to uppercase and print it\r\nprint(s.upper())\r\n\r\n# Convert the string to lowercase and print it\r\nprint(s.lower())\r\n\r\n# Get the length of the string and print it\r\nx = len(s)\r\nprint(x)\r\n\r\n# Count the occurrences of the substring \"Data\" in the string and print the result\r\nprint(s.count(\"Data\"))\r\n\r\n# Find the index of the substring \"Indore\" in the string and print it\r\nprint(s.find(\"Indore\"))\r\n\r\n# Take user input for the name and capitalize the first letter, then print it\r\ns = input(\"Enter your Name:\")\r\nprint(s.capitalize())\r\n\r\n# Take user input for roll number, name, and course\r\nrno = input(\"Enter Roll No\")\r\nname = input(\"Enter Name\")\r\ncourse = input(\"Enter Course\")\r\n\r\n# Format and print a string using the provided roll number, name, and course\r\ns = \"My Roll no is {0} and Name is {1} and Course is {2}\"\r\nprint(s.format(rno, name, course))\r\n\r\n# Convert the given string to title case and print it\r\ns = \"data-Flair provide free python course Data-Flair also having Data related course Data is important\"\r\nprint(s.title())\r\n\r\n# Remove leading and trailing spaces from the string and print it\r\nprint(s.strip())\r\n\r\n# Remove only the leading spaces from the string (Note: This line does not modify the original string)\r\ns.lstrip()\r\n\r\n# Remove only the trailing spaces from the string (Note: This line does not modify the original string)<\/pre>\n<div class=\"df-code-out\">\n<p><strong>Output:<\/strong><\/p>\n<p>DATA-FLAIR<br \/>\ndata-flair<br \/>\n19<br \/>\n1<br \/>\n-1<br \/>\nEnter your Name: ram<br \/>\nRam<br \/>\nEnter Roll No25<br \/>\nEnter Nameram<br \/>\nEnter Coursecomputer science<br \/>\nMy Roll no is 25 and Name is ram and Course is computer science<br \/>\nData-Flair Provide Free Python Course Data-Flair Also Having Data Related Course Data Is Important<br \/>\ndata-Flair provide free Python course Data-Flair also having Data related course Data is important<\/p>\n<h4>Code Explanation:<\/h4>\n<p><strong>1. s.upper()<\/strong><br \/>\nThis converts all the characters in the string to uppercase.<\/p>\n<\/div>\n<ul>\n<li class=\"df-code-out\">For example, &#8220;data-Flair&#8221; would become &#8220;DATA-FLAIR&#8221;<\/li>\n<li class=\"df-code-out\">It returns a new string with the converted case, the original string s remains unchanged.<\/li>\n<\/ul>\n<p><strong>2. s.lower()<\/strong><\/p>\n<ul>\n<li>This converts all characters in the string to lowercase.<\/li>\n<li>For example, &#8220;DATA-FLAIR&#8221; would become &#8220;data-flair&#8221;<\/li>\n<\/ul>\n<p><strong>3. len(s)<\/strong><\/p>\n<ul>\n<li>This returns the length of the string, i.e. the number of characters.<\/li>\n<li>This is assigned to a variable x and printed.<\/li>\n<\/ul>\n<p><strong>4. s.count(&#8220;Data&#8221;)<\/strong><\/p>\n<ul>\n<li>This counts how many times the substring &#8220;Data&#8221; occurs within s.<\/li>\n<li>It performs a case-sensitive search.<\/li>\n<li>The number of occurrences is printed.<\/li>\n<\/ul>\n<p><strong>5. s.find(&#8220;Indore&#8221;)<\/strong><\/p>\n<ul>\n<li>This searches for the substring &#8220;Indore&#8221; within s and returns the index of first match.<\/li>\n<li>If no match is found, it returns -1.<\/li>\n<li>The return value indicates the index position in s where &#8220;Indore&#8221; begins.<\/li>\n<\/ul>\n<p><strong>6. s.capitalize()<\/strong><\/p>\n<ul>\n<li>This capitalizes the first letter of the string while making other letters lowercase.<\/li>\n<li>Takes user input string s, capitalizes first letter and prints the result string.<\/li>\n<\/ul>\n<p><strong>7. s.format()<\/strong><\/p>\n<ul>\n<li>This formats the string using {} placeholders and specified parameters.<\/li>\n<li>User inputs (rno, name, course) are formatted into the result string<\/li>\n<li>Prints formatted string with placeholders replaced by parameters.<\/li>\n<\/ul>\n<p><strong>8. s.title()<\/strong><\/p>\n<ul>\n<li>Converts string s to title case &#8211; first letter of each word is capitalized<\/li>\n<li>For example &#8220;data-flair&#8221; becomes &#8220;Data-Flair&#8221;.<\/li>\n<li>Prints the title case converted string.<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>This article explores various methods of the String class in Python, showcasing their applications for effective string manipulation. The discussed methods cover a range of functionalities, including changing character case, searching for substrings, counting occurrences, and formatting strings. The article provides practical insights for both beginners and intermediate Python developers, assuming a basic understanding of Python syntax and string concepts.<\/p>\n<p>The article also includes prerequisites, suggesting a foundational understanding of Python syntax, variables, and basic string concepts for optimal comprehension. Overall, the provided examples and explanations serve as a valuable resource for those looking to enhance their skills in Python string manipulation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will delve into various methods of the String class in Python, exploring how they can be utilized for effective string manipulation. These methods provide powerful tools for transforming and analyzing&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[28630,28632,28627,28624,28631],"class_list":["post-123212","post","type-post","status-publish","format-standard","hentry","category-python","tag-methods-of-string-class-in-python","tag-python-program-on-methods-of-string-class","tag-python-string-class","tag-python-string-methods","tag-string-class-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Program on Methods of String Class - DataFlair<\/title>\n<meta name=\"description\" content=\"This article explores various methods of the String class in Python, showcasing their applications for effective string manipulation.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-program-on-methods-of-string-class\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Program on Methods of String Class - DataFlair\" \/>\n<meta property=\"og:description\" content=\"This article explores various methods of the String class in Python, showcasing their applications for effective string manipulation.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-program-on-methods-of-string-class\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-02T06:45:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-28T05:39:11+00:00\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Program on Methods of String Class - DataFlair","description":"This article explores various methods of the String class in Python, showcasing their applications for effective string manipulation.","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:\/\/data-flair.training\/blogs\/python-program-on-methods-of-string-class\/","og_locale":"en_US","og_type":"article","og_title":"Python Program on Methods of String Class - DataFlair","og_description":"This article explores various methods of the String class in Python, showcasing their applications for effective string manipulation.","og_url":"https:\/\/data-flair.training\/blogs\/python-program-on-methods-of-string-class\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-11-02T06:45:03+00:00","article_modified_time":"2024-02-28T05:39:11+00:00","author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-methods-of-string-class\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-methods-of-string-class\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Python Program on Methods of String Class","datePublished":"2023-11-02T06:45:03+00:00","dateModified":"2024-02-28T05:39:11+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-methods-of-string-class\/"},"wordCount":598,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["methods of string class in python","python program on methods of string class","python string class","python string methods","string class in python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-program-on-methods-of-string-class\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-methods-of-string-class\/","url":"https:\/\/data-flair.training\/blogs\/python-program-on-methods-of-string-class\/","name":"Python Program on Methods of String Class - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2023-11-02T06:45:03+00:00","dateModified":"2024-02-28T05:39:11+00:00","description":"This article explores various methods of the String class in Python, showcasing their applications for effective string manipulation.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-methods-of-string-class\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-program-on-methods-of-string-class\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-methods-of-string-class\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Program on Methods of String Class"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=123212"}],"version-history":[{"count":4,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123212\/revisions"}],"predecessor-version":[{"id":134111,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123212\/revisions\/134111"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=123212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=123212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=123212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}