Online Tutorials & Training Materials | STechies.com
Register Login

How to Remove Characters from a String in Python?

|| 0

How to Remove Characters from a String in Python?
Stechies

String manipulation is very essential for a python developer. In day to day development task programmer needs to perform string manipulation like:

  • Remove sub string
  • Remove special characters
  • Remove newline character
  • Remove space
  • Remove multiple characters

In this article we have explained important string manipulation with set of examples.

Remove Unwanted or Special Characters from String

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 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 “unwanted” list. In the end we have 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

Removing Spaces from a 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 translate function to replace space with blank character.

Normally translate() function change the character ‘A’ into character ‘B’.

Here by using translate() function we specify the character to translate into 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 Character from String

In the following set of example we have explained how to remove new line character “n” from a string.

Using replace()

As we all know that new line character is defined by “n”, here we are using replace() function to replace all new line character with blank space. By doing this we get the final string without 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 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 pattern using regex() for new line and then we replace all new line “n” character to blank character by using sub() function, by doing this we have final string without new line 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 string

Using replace()

In the following example we have removed 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 specified number of times

Using replace()

In the following example we have removed the character from string in specified number of times by passing the third parameter to specify 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”