Remove Character from String Python
String manipulation is essential for a python developer. In day today, the development task, a programmer needs to perform string manipulation like:
- Remove substring
- Remove special characters
- Remove newline character
- Remove space
- Remove multiple characters
In this article, we have explained significant string manipulation with a set of examples.
Remove Special Characters from String Python
Using replace()
In the following example, we are using replace() function with for loop to check unwanted characters and replace them one by one with a blank character.
Example using replace() :
# Python3 code example
# Remove special unwanted characters
# Using replace()
# Initializing original string
original = "%T;h@is$ t:est ^S~te&ch*ie&s"
# Printing original string
print ("Original String: " + original)
# Initializing unwanted string
unwanted = "!@#$;:!*%)(&^~"
# Run for loop with each character of sting and remove unwanted characters
for char in unwanted:
original = original.replace(char,"")
# Printing final string
print("Final Output: " + original)
Output:
Original String: %T;h@is$ t:est ^S~te&ch*ie&s
Final Output: This test Stechies
Using join()
Using join() function here, we are re-building the string by ignoring unwanted characters from the “unwanted” list. In the end, we have a final string without special characters.
Example using join() :
# Python3 code example
# Remove special unwanted characters
# Using join()
# Initializing original string
original = "%T;h@is$ t:est ^S~te&ch*ie&s"
# Printing original string
print ("Original String: " + original)
# Initializing unwanted string
unwanted = "!@#$;:!*%)(&^~"
# With the help of join we filter unwanted character from string
original = ''.join( c for c in original if c not in unwanted )
# Printing final string
print("Final Output: " + original)
Output:
Original String: %T;h@is$ t:est ^S~te&ch*ie&s
Final Output: This test Stechies
Using Python regex and sub()
Here we are using regx() “Regular Expression” to create a search pattern for space, and with the help of this search pattern, we are replacing the unwanted character from string with blank character by using sub() function.
Example:
# Python3 code example
# Remove unwanted characters
# Using reg() and sub() or Regular Expression
import re
# Initializing original string
original = "%T;h@is$ t:est ^S~te&ch*ie&s"
# Printing original string
print ("Original String: " + original)
# Initializing unwanted string search pattern
unwanted = "[!@#$;:!*%)(&^~]"
# Replace unwanted character from string with blank character by using “Regular Expression”
original = re.sub(unwanted, '', original)
# Printing final string
print("Final Output: " + original)
Output:
Original String: %T;h@is$ t:est ^S~te&ch*ie&s
Final Output: This test Stechies
Python Remove Spaces from String
Using replace()
Here we use replace() function to replace space “ ” with no space,” which will replace all the space between the character in the string with no space.
Example:
# Python3 code example
# Remove space from string
# Using replace()
# Initializing original string
original = "This is test stechies"
# Printing original string
print ("Original String: " + original)
# Replace space with blank character using replace()
original = original.replace(' ', '')
# Printing final string
print("Final Output: " + original)
Output:
Original String: This is test stechies
Final Output: Thisisteststechies
Using translate()
In this example, we are using a translate function to replace space with a blank character.
Usually, translate() function change the character ‘A’ into character ‘B.’
As a result of this using translate() function, we specify the character to translate into a blank character. By doing this, we get the final output with no space.
Example:
# Python3 code example
# Remove space from string
# Using translate()
# Initializing original string
original = "This is test stechies"
# Printing original string
print ("Original String: " + original)
# Replace space with blank character using translate()
original = original.translate({ord(' '): None})
# Printing final string
print("Final Output: " + original)
Output:
Original String: This is test stechies
Final Output: Thisisteststechies
Using split() & join()
In this following example we are using split() & join() function. First, use the split() function to split the string with space then use the join() function to join the string, which will give you the final string with no space.
Example:
# Python3 code example
# Remove space from string
# Using split() & join()
# Initializing original string
original = "This is test stechies"
# Printing original string
print ("Original String: " + original)
# Split the string with space and then join them to make final string
original = "".join(original.split())
# Printing final string
print("Final Output: " + original)
Output:
Original String: This is test stechies
Final Output: Thisisteststechies
Using Python regex() “Regular Expressions” and sub() “Sub-string”
Here we are using regx() “Regular Expression” to create a search pattern for space, and with the help of this search pattern, we are replacing the unwanted character from string with blank character by using sub() function.
Example:
# Python3 code example
# Remove space from string
# Using regex() and sub()
# Import Regular Expressions library
import re
# Define pattern to remove
pattern = re.compile(r's+')
# Initializing original string
original = "This is test stechies"
# Printing original string
print ("Original String: " + original)
# Replace space with blank characters
original = re.sub(pattern, '', original)
# Printing final string
print("Final Output: " + original)
Output:
Original String: This is test stechies
Final Output: Thi i tet techie
Remove Newline from String Python
In the following set of examples, we have explained how to remove the newline character “n” from a string.
Using replace()
As we all know, the new line character is defined by “n,” here we are using replace() function to replace all new line characters with blank space. By doing this, we get the final string without a new line.
Example:
# Python3 code example
# Remove new line n from string
# Using replace()
# Initializing original string
original = "This nis ntest nstechiesn"
# Printing original string
print ("Original String: " + original)
# Replace new line with blank character using replace()
original = original.replace('n', '')
# Printing final string
print("Final Output: " + original)
Output:
Original String: This nis ntest nstechiesn
Final Output: This is test stechies
Using translate()
As we know that translate() function is used to translate the character, so here we translate all “n” to the blank character. By doing this as out, we get the final string without “n.”
Example:
# Python3 code example
# Remove new line'n' from string
# Using translate()
# Initializing original string
original = "This nis ntest nstechies"
# Printing original string
print ("Original String: " + original)
# Replace new line 'n' with blank character using translate()
original = original.translate({ord('n'): None})
# Printing final string
print("Final Output: " + original)
Output:
Original String: This nis ntest nstechies
Final Output: This is test stechies
Using Python regex() “Regular Expressions” and sub() “Sub-string”
In this following example, first, we define search patterns using regex() for a new line, and then we replace all-new line “n” character to blank character by using sub() function. By doing this, we have a final string without a newline character.
Example:
# Python3 code example
# Remove new line 'n' from string
# Using regex() and sub()
import re
# Define pattern to remove
pattern = re.compile(r'n+')
# Initializing original string
original = "This nis ntest nstechies"
# Printing original string
print ("Original String: " + original)
# Replace new line 'n' with blank char ‘’
original = re.sub(pattern, '', original)
# Printing final string
print("Final Output: " + original)
Output:
Original String:
This
is
test
stechies
Final Output: This is test stechies
Remove Sub-string from a String
Using replace()
In the following example, we have removed the sub-string “test” from string “original” by using replace() function.
# Python3 code example
# Remove sub-string from string
# Using replace()
# Initializing original string
original = "This is test stechies"
# Printing original string
print ("Original String: " + original)
# Replace sub-string 'test' with blank character using replace()
original = original.replace('test', '')
# Printing final string
print("Final Output: " + original)
Output:
Original String: This is test stechies
Final Output: This is stechies
Remove Character from String in the Specified Number of times
Using replace()
In the following example, we have removed the character from string in a specified number of times, bypassing the third parameter to specify the number of times replacement with replace() function.
# Python3 code example
# Remove specific character from string with x number of time
# Using replace()
# Initializing original string
original = "This is test stechies"
# Printing original string
print ("Original String: " + original)
# Replace character 'e' with blank character only two time
original = original.replace('e', '', 2)
# Printing final string
print("Final Output: " + original)
Output:
Original String: This is test stechies
Final Output: This is tst stchies
In the above example, you can see that we have removed character “2” two times only from the string “This is test stechies.”