

{"id":123210,"date":"2023-11-02T11:49:18","date_gmt":"2023-11-02T06:19:18","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=123210"},"modified":"2024-01-04T15:24:41","modified_gmt":"2024-01-04T09:54:41","slug":"python-program-string-methods","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-program-string-methods\/","title":{"rendered":"Python Program on String Methods"},"content":{"rendered":"<p>In this article, we will delve into two Python programs that involve string manipulations and checks. These programs demonstrate the flexibility and power of Python when it comes to handling string data.<\/p>\n<p>The first program focuses on analyzing a given string to count the number of spaces, digits, and alphabets. The second program explores various string methods, including checks for uppercase, lowercase, alphanumeric content, numeric properties, and string replacement.<\/p>\n<h3>Prerequisites<\/h3>\n<ul>\n<li>Basic understanding of string manipulation in Python<\/li>\n<li>Familiarity with loops and conditional statements<\/li>\n<li>Knowledge of string methods such as isspace(), isdigit(), isalpha(), isupper(), islower(), isalnum(), isdigit(),<\/li>\n<li>isnumeric(), isdecimal(), and replace()<\/li>\n<li>Basic knowledge of Python syntax<\/li>\n<li>Understanding of input\/output operations in Python<\/li>\n<\/ul>\n<h3>Topic Explanation<\/h3>\n<p><strong>Program 1 &#8211;<\/strong> focuses\u00a0on analyzing user-input strings to gain insights into their composition. It prompts the user to input a string and then employs a for loop to iterate through each character in the string. Using conditional statements and string methods such as isspace(), isdigit(), and isalpha(), the program counts the number of spaces, digits, and alphabets in the input string. The final output displays the counts of spaces, digits, and alphabets, providing a practical example for beginners to understand Python&#8217;s string manipulation capabilities.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Ask the user of input\r\ns = input(\"Enter a String: \")\r\n\r\n# Initialize variables to count spaces, alphabets, and digits\r\nsp = 0\r\nal = 0\r\ndg = 0\r\n\r\n# Iterate through every character in input string\r\nfor i in range(0, len(s)):\r\n    #Check for space\r\n    if s[i].isspace():\r\n        sp = sp + 1\r\n    # Check if the character is a digit\r\n    elif s[i].isdigit():\r\n        dg = dg + 1\r\n    # Check if the character is an alphabet\r\n    elif s[i].isalpha():\r\n        al = al + 1\r\n\r\n# Display counts of spaces, digits, alphabets\r\nprint(\"Total Space is %d\" % sp)\r\nprint(\"Total Digit is %d\" % dg)\r\nprint(\"Total alphabate is %d\" % al)<\/pre>\n<div class=\"df-code-out\"><strong>Output:<\/strong><br \/>\n<strong>Enter a String:<\/strong> DataFlair<br \/>\nTotal Space is 0<br \/>\nTotal Digit is 0<br \/>\nTotal alphabate is 9<\/div>\n<h4>Code Explanation:<\/h4>\n<p>1. Prompt the user to input a string by calling input() and storing it in variable s<br \/>\n2. Initialize variables sp, al, dg to 0 to count spaces, alphabets and digits respectively<br \/>\n3. Iterate through each character in the input string s using a for loop from 0 to length of s<br \/>\n<strong>4. Inside for loop:<\/strong><\/p>\n<ul>\n<li>Check if character is a space using .isspace() and if yes, increment sp counter<\/li>\n<li>Check if character is digit using .isdigit() and if yes, increment dg counter<\/li>\n<li>Check if character is alphabet using .isalpha() and if yes, increment al counter<\/li>\n<\/ul>\n<p>5. After for loop, print the values of sp, dg and al variables to display counts of spaces, digits and alphabets in the input string.<\/p>\n<p><strong>Program 2 &#8211;<\/strong>\u00a0on the other hand, involves a predefined string and showcases a variety of string methods. It checks whether the string is in uppercase or lowercase using isupper() and islower(), respectively. Additionally, it examines if the string contains only alphabetic characters (isalpha()), is alphanumeric (isalnum()), consists of digits (isdigit()), is a numeric value (isnumeric()), and is a decimal value (isdecimal()). The program concludes by demonstrating a string replacement operation, replacing occurrences of &#8220;java&#8221; with &#8220;python&#8221; in the predefined string. This program provides beginners with practical examples of using different string methods in Python.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">s = \"Data-Flair java Free Course java is good language \"\r\n\r\n# Check if the string is in uppercase and print the result\r\nprint(s.isupper())\r\n\r\n# Check if the string is in lowercase and print the result\r\nprint(s.islower())\r\n\r\n# Check if the string contains only alphabets and print the result\r\nif s.isalpha():\r\n    print(\"Valid Name......\")\r\nelse:\r\n    print(\"Invalid Name. Enter again......\")\r\n\r\n# Check if the string is alphanumeric and print the result\r\nprint(s.isalnum())\r\n\r\n# Check if the string consists of digits and print the result\r\nprint(s.isdigit())\r\n\r\n# Check if the string is a numeric value and print the result\r\nprint(s.isnumeric())\r\n\r\n# Check if the string is a decimal and print the result\r\nprint(s.isdecimal())\r\n\r\n# Replace occurrences of \"java\" with \"python\" in the string\r\ns = s.replace(\"java\", \"python\")\r\n\r\n# Print the modified string\r\nprint(s)<\/pre>\n<div class=\"df-code-out\"><strong>Output:<\/strong><br \/>\nFalse<br \/>\nFalse<br \/>\nInvalid Name. Enter again&#8230;&#8230;<br \/>\nFalse<br \/>\nFalse<br \/>\nFalse<br \/>\nFalse<br \/>\nData-Flair python Free Course python is good language<\/div>\n<h4>Code Explanation:<\/h4>\n<div>1. Declare a string s with value &#8220;Data-Flair java Free Course java is good language &#8221;<br \/>\n2. Check if s is in uppercase using .isupper() and print result<br \/>\n3. Check if s is in lowercase using .islower() and print result<br \/>\n4. Check if s contains only alphabets using .isalpha()<\/div>\n<ul>\n<li>If true, print &#8220;Valid Name&#8221;<\/li>\n<li>If false, print &#8220;Invalid Name. Enter again&#8230;&#8230;&#8221;<\/li>\n<\/ul>\n<p>5. Check if s is alphanumeric using .isalnum() and print result<br \/>\n6. Check if s contains only digits using .isdigit() and print result<br \/>\n7. Check if s is numeric value using .isnumeric() and print result<br \/>\n8. Check if s is decimal value using .isdecimal() and print result<br \/>\n9. Replace &#8220;java&#8221; with &#8220;python&#8221; in s using .replace()<br \/>\n10. Print the modified string s<\/p>\n<h3>Summary<\/h3>\n<p>In these Python programs, the first one analyzes user-input strings to count spaces, digits, and alphabets. The second program checks predefined strings for properties like uppercase, lowercase, alphanumeric content, and performs string replacement, showcasing Python&#8217;s string manipulation capabilities. These Python programs deepen our grasp of fundamental Python concepts, particularly in manipulating strings. The detailed explanations cover essential aspects of Python syntax and string operations, contributing to a broader understanding of the language.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will delve into two Python programs that involve string manipulations and checks. These programs demonstrate the flexibility and power of Python when it comes to handling string data. The first&#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":[10333,28626,28627,28625,28624,10865,28628,28629],"class_list":["post-123210","post","type-post","status-publish","format-standard","hentry","category-python","tag-python","tag-python-practical","tag-python-string-class","tag-python-string-function","tag-python-string-methods","tag-python-strings","tag-string-functions-in-python","tag-string-methods-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 String Methods - DataFlair<\/title>\n<meta name=\"description\" content=\"These Python programs demonstrate the flexibility and power of Python when it comes to handling string data.\" \/>\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-string-methods\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Program on String Methods - DataFlair\" \/>\n<meta property=\"og:description\" content=\"These Python programs demonstrate the flexibility and power of Python when it comes to handling string data.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-program-string-methods\/\" \/>\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:19:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-04T09:54:41+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 String Methods - DataFlair","description":"These Python programs demonstrate the flexibility and power of Python when it comes to handling string data.","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-string-methods\/","og_locale":"en_US","og_type":"article","og_title":"Python Program on String Methods - DataFlair","og_description":"These Python programs demonstrate the flexibility and power of Python when it comes to handling string data.","og_url":"https:\/\/data-flair.training\/blogs\/python-program-string-methods\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-11-02T06:19:18+00:00","article_modified_time":"2024-01-04T09:54:41+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-string-methods\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-string-methods\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Python Program on String Methods","datePublished":"2023-11-02T06:19:18+00:00","dateModified":"2024-01-04T09:54:41+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-string-methods\/"},"wordCount":635,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["Python","python practical","python string class","python string function","python string methods","python strings","string functions in python","string methods in python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-program-string-methods\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-program-string-methods\/","url":"https:\/\/data-flair.training\/blogs\/python-program-string-methods\/","name":"Python Program on String Methods - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2023-11-02T06:19:18+00:00","dateModified":"2024-01-04T09:54:41+00:00","description":"These Python programs demonstrate the flexibility and power of Python when it comes to handling string data.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-string-methods\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-program-string-methods\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-program-string-methods\/#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 String Methods"}]},{"@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\/123210","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=123210"}],"version-history":[{"count":6,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123210\/revisions"}],"predecessor-version":[{"id":132849,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123210\/revisions\/132849"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=123210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=123210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=123210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}