Isascii method of str class in Python

Method Name:

isascii

Method Signature:

isascii()

Parameters:

None

Return Value:

True, if the string is made of all ASCII characters or if the string is empty.

False, if one or more characters of the string is a non-ASCII character.

Method:

isascii()

Overview:

  • The isascii() method tests whether the characters of a string is all ASCII or not and returns a Boolean value based on the test result.
  • The isascii() method returns True for an empty Python string.

Example:

# Example Python program to test whether a string is composed full of ASCII characters

# or not

 

# Test a string of katakana characters

katakanaString  = "片仮名";

asciiTestResult = katakanaString.isascii();

print("\"%s\", consists of one or more ASCII characters:%s"%(katakanaString, asciiTestResult));

 

# Test an empty string

emptyString = '';

asciiTestResult = emptyString.isascii();

print("An empty Python string passes ASCII test:%s"%(asciiTestResult));

 

# Test an ASCII string

asciiText = "Hello World";

asciiTestResult = asciiText.isascii();

print("\"%s\", consists of one or more ASCII characters:%s"%(asciiText, asciiTestResult));

 

Output:

"片仮名", consists of one or more ASCII characters:False

An empty Python string passes ASCII test:True

"Hello World", consists of one or more ASCII characters:True


Copyright 2024 © pythontic.com