{"id":3655,"date":"2023-04-27T23:32:13","date_gmt":"2023-04-27T18:02:13","guid":{"rendered":"https:\/\/cbsepython.in\/?p=3655"},"modified":"2023-07-10T09:45:46","modified_gmt":"2023-07-10T04:15:46","slug":"python-string-exercise-for-absolute-beginners","status":"publish","type":"post","link":"https:\/\/cbsepython.in\/python-string-exercise-for-absolute-beginners\/","title":{"rendered":"(Solved) Python String Exercise For Absolute Beginners"},"content":{"rendered":"<h2>Python String Exercise-1<\/h2>\n<p>Here&#8217;s an exercise for beginners on creating and manipulating strings in Python:<\/p>\n<ol>\n<li>Create a variable called <code>name<\/code> and assign it a string value representing your name.<\/li>\n<li>Use the <code>len()<\/code> function to find the length of your name and assign the result to a variable called <code>name_length<\/code>.<\/li>\n<li>Print a message to the console that says &#8220;My name is [name] and it has [name_length] letters.&#8221; Use string concatenation to combine the values of <code>name<\/code> and <code>name_length<\/code> into a single string.<\/li>\n<li>Create a new variable called <code>favorite_food<\/code> and assign it a string value representing your favorite food.<\/li>\n<li>Use string formatting to print a message to the console that says &#8220;I love to eat [favorite_food]!&#8221;.<\/li>\n<li>Use the <code>split()<\/code> method to split the value of <code>favorite_food<\/code> into a list of words and assign the result to a variable called <code>food_list<\/code>.<\/li>\n<li>Print the first word in <code>food_list<\/code> to the console.<\/li>\n<li>Use the <code>join()<\/code> method to combine the words in <code>food_list<\/code> into a single string, separated by commas, and assign the result to a variable called <code>food_string<\/code>.<\/li>\n<li>Print <code>food_string<\/code> to the console.<\/li>\n<\/ol>\n<h2><\/h2>\n<h4>Solution:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Step 1\r\nname = \"John\"\r\n\r\n# Step 2\r\nname_length = len(name)\r\n\r\n# Step 3\r\nprint(\"My name is \" + name + \" and it has \" + str(name_length) + \" letters.\")\r\n\r\n# Step 4\r\nfavorite_food = \"pizza\"\r\n\r\n# Step 5\r\nprint(\"I love to eat {}!\".format(favorite_food))\r\n\r\n# Step 6\r\nfood_list = favorite_food.split()\r\n\r\n# Step 7\r\nprint(food_list[0])\r\n\r\n# Step 8\r\nfood_string = \", \".join(food_list)\r\n\r\n# Step 9\r\nprint(food_string)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2><\/h2>\n<h2>Python String Exercise-2<\/h2>\n<p>&nbsp;<\/p>\n<p>here&#8217;s another exercise on strings in Python for beginners:<\/p>\n<ol>\n<li>Create a variable called <code>word<\/code> and assign it a string value representing a word of your choice.<\/li>\n<li>Print the length of the word to the console using the <code>len()<\/code> function.<\/li>\n<li>Use string indexing to print the first character of the word to the console.<\/li>\n<li>Use string slicing to print the first three characters of the word to the console.<\/li>\n<li>Use string slicing to print the last three characters of the word to the console.<\/li>\n<li>Use string slicing to print every other character of the word, starting from the second character, to the console.<\/li>\n<li>Use the <code>in<\/code> keyword to check if the letter &#8220;e&#8221; is in the word. Print the result to the console.<\/li>\n<li>Use string concatenation and repetition to create a new string that repeats the word three times, separated by spaces, and assign it to a variable called <code>new_word<\/code>.<\/li>\n<li>Print <code>new_word<\/code> to the console.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4>Solution:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Step 1\r\nword = \"python\"\r\n\r\n# Step 2\r\nprint(len(word))\r\n\r\n# Step 3\r\nprint(word[0])\r\n\r\n# Step 4\r\nprint(word[:3])\r\n\r\n# Step 5\r\nprint(word[-3:])\r\n\r\n# Step 6\r\nprint(word[1::2])\r\n\r\n# Step 7\r\nif \"e\" in word:\r\n    print(\"The letter 'e' is in the word.\")\r\nelse:\r\n    print(\"The letter 'e' is not in the word.\")\r\n\r\n# Step 8\r\nnew_word = word + \" \" + word + \" \" + word\r\nprint(new_word)\r\n\r\n# Step 9\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python String Exercise-1 Here&#8217;s an exercise for beginners on creating and manipulating strings in Python: Create a variable called name and assign it a string value representing your name. Use the len() function to find the length of your name and assign the result to a variable called name_length. Print a message to the console [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-3655","post","type-post","status-publish","format-standard","hentry","category-cbse-computer-science-with-python-class-12"],"_links":{"self":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/3655","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/comments?post=3655"}],"version-history":[{"count":0,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/3655\/revisions"}],"wp:attachment":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/media?parent=3655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/categories?post=3655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/tags?post=3655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}