The following methods are used to remove a specific character from a string in Python. Note that the string is immutable in Python. So the original string
remains unchanged and a new string is returned by these methods. In this method, we have to run a loop and append the characters and build a new string from the existing characters except when the index is n. (where n is the index of the character to be removed) Output: Original string: DivasDwivedi 2. Removal of Character from a String using replace() Methodstr = "Engineering" print ("Original string: " + str) res_str = str.replace('e', '') # removes all occurrences of 'e' print ("The string after removal of character: " + res_str) # Removing 1st occurrence of e res_str = str.replace('e', '', 1) print ("The string after removal of character: " + res_str) Output: Original string: Engineering 3. Removal of Character from a String using Slicing and Concatenationstr = "Engineering" print ("Original string: " + str) # Removing char at pos 3 # using slice + concatenation res_str = str[:2] + str[3:] print ("String after removal of character: " + res_str) Output: Original string: Engineering 4. Removal of Character from a String using join() method and list comprehensionIn this technique, every element of the string is converted to an equivalent element of a list, after which each of them is joined to form a string excluding the particular character to be removed. str = "Engineering" print ("Original string: " + str) # Removing char at index 2 # using join() + list comprehension res_str = ''.join([str[i] for i in range(len(str)) if i != 2]) print ("String after removal of character: " + res_str) Output: Original string: Engineering 5. Removal of character from a string using translate() methodstr = 'Engineer123Discipline' print(str.translate({ord(i): None for i in '123'})) Output: EngineerDiscipline References
Sometimes we want to remove all occurrences of a character from a string. There are two common ways to achieve this. Python Remove Character from String
Python Remove Character from String using replace()We can use string replace() function to replace a character with a new character. If we provide an empty string as the second argument, then the character will get removed from the string. Note that the string is immutable in Python, so this function will return a new string and the original string will remain unchanged.
Output: Python Remove Character from String using translate()Python string translate() function replace each character in the string using the given translation table. We have to specify the Unicode code point for the character and ‘None’ as a replacement to remove it from the result string. We can use ord() function to get the Unicode code point of a character.
Output:
Output: Removing Spaces from a String
Python Remove newline from String
Remove substring from stringString replace() function arguments is string. Let’s see how to remove a word from a string.
Output: Remove specified number of timesWe can also pass a third parameter in replace() function to specify the number of times replacement should be performed.
Output: You can checkout complete python script and more Python examples from our GitHub Repository. How do you remove part of a string from a string?We can remove part of the string using REPLACE() function. We can use this function if we know the exact character of the string to remove. REMOVE(): This function replaces all occurrences of a substring within a new substring.
How do I remove a specific element from a string in Python?Using translate():
translate() is another method that can be used to remove a character from a string in Python. translate() returns a string after removing the values passed in the table. Also, remember that to remove a character from a string using translate() you have to replace it with None and not "" .
How do I remove a character from a string?You can also remove a specified character or substring from a string by calling the String. Replace(String, String) method and specifying an empty string (String. Empty) as the replacement.
|