isascii method of str class in Python

The isascii() method returns True, if a string is made of one or more ASCII characters. In other words it returns False, if at least one character in a string is a non-ASCII character.

Str Updated 18 Nov 2019 1 min read

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

python
# 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

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