Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python Program To Remove all control characters

  • Last Updated : 21 Jul, 2021

In the telecommunication and computer domain, control characters are non-printable characters which are a part of the character set. These do not represent any written symbol. They are used in signaling to cause certain effects other than adding symbols to text. Removing these control characters is an essential utility. In this article, we will discuss how to remove all those control characters.

Example:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Input : test_str = ‘Geeks\0\r for \n\bge\tee\0ks\f’



Output : Geeks for geeeks

Explanation : \n, \0, \f, \r, \b, \t being control characters are removed from string.

Input : test_str = ‘G\0\r\n\fg’

Output : Gfg

Explanation : \n, \0, \f, \r being control characters are removed from string, giving Gfg as output.

Method 1 : Using translate().

The logic applied here is that each non-control character is at the top 33 ASCII characters, hence translation is used to avoid all others except these via mapping.

Python3




# Python3 code to demonstrate working of
# Remove all control characters
# Using translate()
  
# initializing string
test_str = 'Geeks\0\r for \n\bge\tee\0ks\f'
  
# printing original string
print("The original string is : " + str(test_str))
  
# using translate() and fromkeys()
# to escape all control characters
mapping =  dict.fromkeys(range(32))
res = test_str.translate(mapping)
  
# printing result
print("String after removal of control characters : " + str(res))


Output:

 for riginal string is : Geeks
ge    eeks
String after removal of control characters : Geeks for geeeks

Method 2: Using unicodedata library

In this, using unicodedata.category(), we can check each character starting with “C” is the control character and hence be avoided in the result string.

Python3




# Python3 code to demonstrate working of
# Remove all control characters
# Using unicodedata library
import unicodedata
  
# initializing string
test_str = 'Geeks\0\r for \n\bge\tee\0ks\f'
  
# printing original string
print("The original string is : " + str(test_str))
  
# surpassing all control characters
# checking for starting with C
res = "".join(char for char in test_str if unicodedata.category(char)[0]!="C")
  
# printing result
print("String after removal of control characters : " + str(res))


Output:

 for riginal string is : Geeks
ge    eeks
String after removal of control characters : Geeks for geeeks



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!