Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Check for ASCII string

Improve Article
Save Article
  • Last Updated : 11 Jan, 2023
Improve Article
Save Article

Many times it is desirable to work with the strings that only contain alphabets and the other special characters are undesirable and sometimes this very task becomes the point to filter the strings and hence requires the way to check if a string is whole ASCII. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using ord() + all() The combination of this method can be used to achieve the desirable task. In this method, we search for all the string and check for each character, a value in range of ASCII characters. 

Python3




# Python3 code to demonstrate
# Check for ASCII string
# using all() + ord()
 
# initializing string
test_string = "G4G is best"
 
# printing original string
print("The original string : " + str(test_string))
 
# using all() + ord()
# Check for ASCII string
res = all(ord(c) < 128 for c in test_string)
 
# print result
print("Is the string full ASCII ? : " + str(res))

Output : 

The original string : G4G is best
Is the string full ASCII ? : True

  Method #2 : Using lambda + encode() This task can also be achieved using the above functions. In this combination, lambda function is used to extend the size check logic to whole string and encode function checks if the size of original and encoded strings match. 

Python3




# Python3 code to demonstrate
# Check for ASCII string
# using lambda + encode()
 
# initializing string
test_string = "G4G is best"
 
# printing original string
print("The original string : " + str(test_string))
 
# using lambda + encode()
# Check for ASCII string
res = lambda ele: len(ele) == len(ele.encode())
 
# print result
print("Is the string full ASCII ? : " + str(res(test_string)))

Output : 

The original string : G4G is best
Is the string full ASCII ? : True

Method 3: Using re.search()
re.search() is a function from the Python re (regular expression) module. It searches for a pattern (in this case, a pattern that matches any ASCII character) within a string and returns a match object if found, or None if not found.

Python




import re  # Import the re module
 
def is_ascii(string):
  # Use re.search() to search for a pattern (in this case, a pattern that matches any ASCII character)
  # within the input string. The r prefix before the pattern string tells Python to treat the string as a raw string,
  # which is useful for patterns that contain backslashes. The ^ and $ symbols indicate that the pattern should
  # match the entire string (not just a substring).
  match = re.search(r'^[\x00-\x7F]+$', string)
   
  # Return a boolean value indicating whether a match was found (True if a match was found, False if not)
  return bool(match)
 
# Test the function
test_string = "G4G is best"
print("The original string : " + str(test_string))
print("Is the string full ASCII ? :", is_ascii(test_string))
#This code is contributed by Edula Vinay Kumar Reddy

Output

The original string : G4G is best
('Is the string full ASCII ? :', True)

This function has a time complexity of O(n), where n is the length of the input string, since re.search() searches through the entire string. The auxiliary space is O(1), since the function only creates a single boolean variable to store the result.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!