{"id":10328,"date":"2021-09-30T12:00:49","date_gmt":"2021-09-30T06:30:49","guid":{"rendered":"https:\/\/python-programs.com\/?p=10328"},"modified":"2021-11-22T18:34:40","modified_gmt":"2021-11-22T13:04:40","slug":"python-program-to-remove-the-characters-of-odd-index-values-in-a-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-remove-the-characters-of-odd-index-values-in-a-string\/","title":{"rendered":"Python Program to Remove the Characters of Odd Index Values in a String"},"content":{"rendered":"
Strings in Python:<\/strong><\/p>\n A string is a series of one or more characters (letters, integers, symbols) that might be constant or variable. Strings, which are made up of Unicode, are immutable sequences, which means they do not change.<\/p>\n Because the text is such a frequent type of data in our daily lives, the string data type is a critical building block of programming.<\/p>\n Given a string, the task is to remove all the characters present at odd indices in the given string in Python<\/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 There are several ways to remove all the characters present at odd indices in the given string in Python some of them are:<\/p>\n The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples<\/a> as it includes basic to advanced levels of concepts.<\/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 Explanation:<\/strong><\/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 Related Programs<\/strong>:<\/p>\n Strings in Python: A string is a series of one or more characters (letters, integers, symbols) that might be constant or variable. Strings, which are made up of Unicode, are immutable sequences, which means they do not change. Because the text is such a frequent type of data in our daily lives, the string data …<\/p>\ngiven string = HelloThisisBTechgeeks<\/pre>\n
The given string before modification = HelloThisisBTechgeeks\r\nThe given string after modification = HlohssTcges<\/pre>\n
given string = btechgeeks online coding platform for coding studeents<\/pre>\n
Enter the given random string = btechgeeks online coding platform for coding studeents\r\nThe given string before modification = btechgeeks online coding platform for coding studeents\r\nThe given string after modification = behek niecdn ltomfrcdn tdet\r\n<\/pre>\n
Program to Remove the Characters of Odd Index Values in a String in Python<\/h2>\n
\n
Method #1)Using Modulus operator and String Concatenation(Static Input)<\/h3>\n
\n
# function that accepts the string as an argument\r\n# and removes all the characters present at odd indices in the given string.\r\n\r\n\r\ndef removeOddString(givenstrng):\r\n # In the function take an empty string say tempstng using '' .\r\n tempstng = \"\"\r\n # Traverse the given string using For loop.\r\n for charindex in range(len(givenstrng)):\r\n # Use an if statement and modulus operator to\r\n # determine whether the string's index is odd or even.\r\n if charindex % 2 == 0:\r\n # If the index is an even number, append the\r\n # character to the tempstng using String Concatenation.\r\n tempstng = tempstng + givenstrng[charindex]\r\n # Return the tempstng.\r\n return tempstng\r\n\r\n\r\n# Give the string as static input and store it in a variable.\r\ngivenstrng = 'HelloThisisBTechgeeks'\r\n# print the given string without modification'\r\nprint('The given string before modification = ', givenstrng)\r\n# Pass the given string as an argument to the removeOddString function.\r\nresstrng = removeOddString(givenstrng)\r\n# print the given string after modification\r\nprint('The given string after modification = ', resstrng)\r\n<\/pre>\n
The given string before modification = HelloThisisBTechgeeks\r\nThe given string after modification = HlohssTcges<\/pre>\n
Method #2)Using Modulus operator and String Concatenation(User Input)<\/h3>\n
\n
# function that accepts the string as an argument\r\n# and removes all the characters present at odd indices in the given string.\r\n\r\n\r\ndef removeOddString(givenstrng):\r\n # In the function take an empty string say tempstng using '' .\r\n tempstng = \"\"\r\n # Traverse the given string using For loop.\r\n for charindex in range(len(givenstrng)):\r\n # Use an if statement and modulus operator to\r\n # determine whether the string's index is odd or even.\r\n if charindex % 2 == 0:\r\n # If the index is an even number, append the\r\n # character to the tempstng using String Concatenation.\r\n tempstng = tempstng + givenstrng[charindex]\r\n # Return the tempstng.\r\n return tempstng\r\n\r\n\r\n# Give the string as the user input with the help of input() function and store it in a variable.\r\ngivenstrng = input('Enter the given random string = ')\r\n# print the given string without modification'\r\nprint('The given string before modification = ', givenstrng)\r\n# Pass the given string as an argument to the removeOddString function.\r\nresstrng = removeOddString(givenstrng)\r\n# print the given string after modification\r\nprint('The given string after modification = ', resstrng)\r\n<\/pre>\n
Enter the given random string = btechgeeks online coding platform for coding studeents\r\nThe given string before modification = btechgeeks online coding platform for coding studeents\r\nThe given string after modification = behek niecdn ltomfrcdn tdet\r\n\r\n<\/pre>\n
\n
Method #3:Using List Comprehension and Join function(Static Input)<\/h3>\n
\n
# function that accepts the string as an argument\r\n# and removes all the characters present at odd indices in the given string.\r\n\r\n\r\ndef removeOddString(givenstrng):\r\n # Convert the given string into a list of characters using the list() function.\r\n liststrng = list(givenstrng)\r\n # Using list comprehension with the help of the if and modulus operator eliminates the odd index characters.\r\n liststrng = [givenstrng[inde]\r\n for inde in range(len(givenstrng)) if inde % 2 == 0]\r\n # Join the list into the string using the join() ffunction.\r\n tempstng = ''.join(liststrng)\r\n # Return the tempstng.\r\n return tempstng\r\n\r\n\r\n# Give\u00a0the\u00a0string\u00a0as\u00a0static\u00a0input\u00a0and\u00a0store\u00a0it\u00a0in\u00a0a\u00a0variable.\r\ngivenstrng = 'Hello this is BTechGeeks online coding platform for coding students'\r\n# print the given string without modification'\r\nprint('The given string before modification = ', givenstrng)\r\n# Pass the given string as an argument to the removeOddString function.\r\nresstrng = removeOddString(givenstrng)\r\n# print the given string after modification\r\nprint('The given string after modification = ', resstrng)\r\n<\/pre>\n
The given string before modification = Hello this is BTechGeeks online coding platform for coding students\r\nThe given string after modification = Hloti sBehek niecdn ltomfrcdn tdns<\/pre>\n
Method #4:Using List Comprehension and Join function(User Input)<\/h3>\n
\n
# function that accepts the string as an argument\r\n# and removes all the characters present at odd indices in the given string.\r\n\r\n\r\ndef removeOddString(givenstrng):\r\n # Convert the given string into a list of characters using the list() function.\r\n liststrng = list(givenstrng)\r\n # Using list comprehension with the help of the if and modulus operator eliminates the odd index characters.\r\n liststrng = [givenstrng[inde]\r\n for inde in range(len(givenstrng)) if inde % 2 == 0]\r\n # Join the list into the string using the join() ffunction.\r\n tempstng = ''.join(liststrng)\r\n # Return the tempstng.\r\n return tempstng\r\n\r\n\r\n# Give the string as the user input with the help of input() function and store it in a variable.\r\ngivenstrng = input('Enter the given random string = ')\r\n# print the given string without modification'\r\nprint('The given string before modification = ', givenstrng)\r\n# Pass the given string as an argument to the removeOddString function.\r\nresstrng = removeOddString(givenstrng)\r\n# print the given string after modification\r\nprint('The given string after modification = ', resstrng)\r\n<\/pre>\n
Enter the given random string = good morning this is btechgeeks\r\nThe given string before modification = good morning this is btechgeeks\r\nThe given string after modification = go onn hsi tcges<\/pre>\n
\n