{"id":10736,"date":"2021-09-30T12:00:20","date_gmt":"2021-09-30T06:30:20","guid":{"rendered":"https:\/\/python-programs.com\/?p=10736"},"modified":"2021-11-22T18:34:40","modified_gmt":"2021-11-22T13:04:40","slug":"python-program-to-remove-adjacent-duplicate-characters-from-a-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-remove-adjacent-duplicate-characters-from-a-string\/","title":{"rendered":"Python Program to Remove Adjacent Duplicate Characters from a String | How to Remove All Adjacent Duplicates from a String?"},"content":{"rendered":"
Are you looking for help to remove all the adjacent duplicate characters in a string? Then, this tutorial can be extremely helpful for you as we have compiled all about how to remove adjacent duplicate characters from a string in Python clearly. Refer to the Sample Programs for removing all adjacent duplicates from a string and the function used for doing so.<\/p>\n
Given a string, which contains duplicate characters the task is to remove the adjacent duplicate characters from the given string.<\/p>\n
Examples:<\/strong><\/p>\n Example 1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below is the full approach to remove the adjacent duplicate characters from the given string in Python.<\/p>\n Explore complete java concepts from the Java programming examples<\/a> and get ready to become a good programmer and crack the java software developer interview with ease.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n <\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n <\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Are you looking for help to remove all the adjacent duplicate characters in a string? Then, this tutorial can be extremely helpful for you as we have compiled all about how to remove adjacent duplicate characters from a string in Python clearly. Refer to the Sample Programs for removing all adjacent duplicates from a string …<\/p>\ngiven string =bteechhgeeeekkkkssss<\/pre>\n
given string before removing adjacent duplicate characters = bteechhgeeeekkkkssss\r\ngiven string without after adjacent duplicate characters = btechgeks<\/pre>\n
given string ='appplussstoppperr'<\/pre>\n
given string before removing adjacent duplicate characters = appplussstoppperr\r\ngiven string without after adjacent duplicate characters = aplustoper<\/pre>\n
How to Remove Adjacent Duplicate Characters from a String in Python?<\/h2>\n
\n
1)Using For loop and If statements(Static Input)<\/h3>\n
\n
# Function to remove adjacent duplicates characters from a string\r\ndef remAdj(givenstrng):\r\n # convert the given string to list using list() function\r\n charslist = list(givenstrng)\r\n prevele = None\r\n p = 0\r\n # Traverse the given string\r\n for chars in givenstrng:\r\n if prevele != chars:\r\n charslist[p] = chars\r\n prevele = chars\r\n p = p + 1\r\n # join the list which contains characters to string using join function and return it\r\n return ''.join(charslist[:p])\r\n\r\n\r\n# Driver code\r\n# Give the string as static input and store it in a variable.\r\ngivenstrng = \"bteechhgeeeekkkkssss\"\r\n# printing the given string before removing adjacent duplicate characters\r\nprint('given string before removing adjacent duplicate characters = ', givenstrng)\r\n# Pass the given string to the remAdj function which accepts\r\n# the given string as the argument\r\n# and returns the modified string with no adjacent duplicates.\r\nmodistring = remAdj(givenstrng)\r\n# printing the given string after removing adjacent duplicate characters\r\nprint('given string without after adjacent duplicate characters = ', modistring)\r\n<\/pre>\n
given string before removing adjacent duplicate characters = bteechhgeeeekkkkssss\r\ngiven string without after adjacent duplicate characters = btechgeks<\/pre>\n
2)Using For loop and If statements(User Input)<\/h3>\n
\n
# Function to remove adjacent duplicates characters from a string\r\ndef remAdj(givenstrng):\r\n # convert the given string to list using list() function\r\n charslist = list(givenstrng)\r\n prevele = None\r\n p = 0\r\n # Traverse the given string\r\n for chars in givenstrng:\r\n if prevele != chars:\r\n charslist[p] = chars\r\n prevele = chars\r\n p = p + 1\r\n # join the list which contains characters to string using join function and return it\r\n return ''.join(charslist[:p])\r\n\r\n\r\n# Driver code\r\n# Give the string as user input using the input() function.\r\n# Store it in a variable.\r\ngivenstrng = input('Enter some random string = ')\r\n# printing the given string before removing adjacent duplicate characters\r\nprint('given string before removing adjacent duplicate characters = ', givenstrng)\r\n# Pass the given string to the remAdj function which accepts\r\n# the given string as the argument\r\n# and returns the modified string with no adjacent duplicates.\r\nmodistring = remAdj(givenstrng)\r\n# printing the given string after removing adjacent duplicate characters\r\nprint('given string without after adjacent duplicate characters = ', modistring)\r\n<\/pre>\n
Enter some random string = appplussstoppperr\r\ngiven string before removing adjacent duplicate characters = appplussstoppperr\r\ngiven string without after adjacent duplicate characters = aplustoper<\/pre>\n
\n