{"id":18715,"date":"2021-08-26T10:38:04","date_gmt":"2021-08-26T05:08:04","guid":{"rendered":"https:\/\/python-programs.com\/?p=18715"},"modified":"2021-11-22T18:37:18","modified_gmt":"2021-11-22T13:07:18","slug":"python-program-for-snake-case-of-a-given-sentence","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-snake-case-of-a-given-sentence\/","title":{"rendered":"Python Program for Snake Case of a Given Sentence"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Check Trimorphic Number or Not<\/a> \u00a0The lower() method in Python:<\/strong><\/p>\n lower() returns a string with all characters in lower case.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to remove spaces from a sentence and rewrite it in the Snake case:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Check Trimorphic Number or Not The task is to remove spaces from a sentence and rewrite it in the Snake case. It is a writing style in which spaces are replaced with underscores and all words begin with small letters. \u00a0The lower() method in Python: …<\/p>\n
\nThe task is to remove spaces from a sentence and rewrite it in the Snake case. It is a writing style in which spaces are replaced with underscores and all words begin with small letters.<\/p>\nGiven string\/sentence = Hello this is Btechgeeks<\/pre>\n
The given sentence { Hello this is Btechgeeks } after Converting into Snake case :\r\nhello_this_is_btechgeeks<\/pre>\n
Given string\/sentence = Good morning This is Btechgeeks<\/pre>\n
The given sentence { Good morning This is Btechgeeks } after Converting into Snake case :\r\ngood_morning_this_is_btechgeeks<\/pre>\n
Program for Snake Case of a Given Sentence in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the string\/sentence as static input and store it in a variable.\r\ngvn_strng = \"Hello this is Btechgeeks\"\r\n# Calculate the length of the given string using the len() function and\r\n# store it in another variable.\r\nstr_lengt = len(gvn_strng)\r\n# Convert the given string into a list of characters using the list() function and\r\n# store it in another variable say lst_strng.\r\nlst_strng = list(gvn_strng)\r\n# Iterate up to the length of the given string using the for loop.\r\nprint(\"The given sentence {\", gvn_strng,\r\n \"} after Converting into Snake case :\")\r\nfor itr in range(str_lengt):\r\n # Check if the element of the list of characters ( lst_strng ) is equal to space ( ' ' )\r\n # using the if conditional statement.\r\n if (lst_strng[itr] == ' '):\r\n # If the statement is true, then replace the space with the underscore( _ ).\r\n lst_strng[itr] = '_'\r\n else:\r\n # If the statement is false, then convert the elements of the list of characters ( lst_strng )\r\n # into lower case using the lower() function.\r\n lst_strng[itr] = lst_strng[itr].lower()\r\n# Convert the above list lst_strng into the string using the join function.\r\n# Store it in the same variable lst_strng.\r\nlst_strng = \"\".join(lst_strng)\r\n# Print the lst_strng to remove spaces from a given sentence and rewrite it in the\r\n# Snake case.\r\nprint(lst_strng)\r\n<\/pre>\n
The given sentence { Hello this is Btechgeeks } after Converting into Snake case :\r\nhello_this_is_btechgeeks<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the string\/sentence as user input using the input() function\r\n# and store it in a variable.\r\ngvn_strng = input(\"Enter some random string= \")\r\n# Calculate the length of the given string using the len() function and\r\n# store it in another variable.\r\nstr_lengt = len(gvn_strng)\r\n# Convert the given string into a list of characters using the list() function and\r\n# store it in another variable say lst_strng.\r\nlst_strng = list(gvn_strng)\r\n# Iterate up to the length of the given string using the for loop.\r\nprint(\"The given sentence {\", gvn_strng,\r\n \"} after Converting into Snake case :\")\r\nfor itr in range(str_lengt):\r\n # Check if the element of the list of characters ( lst_strng ) is equal to space ( ' ' )\r\n # using the if conditional statement.\r\n if (lst_strng[itr] == ' '):\r\n # If the statement is true, then replace the space with the underscore( _ ).\r\n lst_strng[itr] = '_'\r\n else:\r\n # If the statement is false, then convert the elements of the list of characters ( lst_strng )\r\n # into lower case using the lower() function.\r\n lst_strng[itr] = lst_strng[itr].lower()\r\n# Convert the above list lst_strng into the string using the join function.\r\n# Store it in the same variable lst_strng.\r\nlst_strng = \"\".join(lst_strng)\r\n# Print the lst_strng to remove spaces from a given sentence and rewrite it in the\r\n# Snake case.\r\nprint(lst_strng)\r\n<\/pre>\n
Enter some random string= Good morning This is Btechgeeks\r\nThe given sentence { Good morning This is Btechgeeks } after Converting into Snake case :\r\ngood_morning_this_is_btechgeeks<\/pre>\n
\n