Python string isascii() method

Python String isascii() Method

Description

The isascii() method returns whether all characters in a string are in the ASCII character set. ASCII characters have values between 0 and 127. Their corresponding Unicode values are between u0000 and u007f. Any character outside this range is not an ASCII character. Therefore, if the string contains any such characters, the ascii() method returns False.

Syntax

The syntax of the isascii() method is as follows:

var.isascii()

Parameters

None

Return Value

This method returns true if all characters in the string are ASCII characters. Otherwise, it returns false.

Example

The following example demonstrates the use of the isascii() method.

var = "Hello Python"
var1 = var.isascii()
print ("Original string:", var)
print ("Only ASCII characters?:", var1)

var = "ABC!@#"
var2 = var.isascii()
print ("Original string:", var)
print ("Only ASCII characters?:", var2)

var = "u20B9100"
var3 = var.isascii()
print ("Original string:", var)
print ("Only ASCII characters?:", var3)

When you run this program, it will produce the following output −

Original string: Hello Python
Only ASCII characters? : True
Original string: ABC!@#
Only ASCII characters? : True
Original string: ₹100
Only ASCII characters? : False

20B9 is the UNICODE for ₹, which is a non-ASCII character.

Leave a Reply

Your email address will not be published. Required fields are marked *