String isascii() Method in Python:
If all of the characters are ASCII, the isascii() method returns True (a-z). Otherwise returns False.
Syntax:
string.isascii()
Parameters: This method has no parameters.
Examples:
Example1:
Input:
Given string = "hello@%btechgeeks670"
Output:
True
Example2:
Input:
Given string = 'ß'
Output:
False
String isascii() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Apply isascii() method on the given string where if all of the characters are ASCII, the isascii() method returns True (a-z). Otherwise returns False.
- Store it in a variable.
- Print the result after applying isascii() method on the given string.
- The Exit of the Program.
Below is the implementation:
# Give the string as static input and store it in a variable. gvn_str = "btechgeeks789@" # Apply isascii() method on the given string where if all of the characters are # ASCII, the isascii() method returns True (a-z). Otherwise returns False. # Store it in a variable. rslt = gvn_str.isascii() # Print the result after applying isascii() method on the given string. print(rslt)
Output:
True
For any non-ASCII characters, the isascii() method returns False.
For Example:
# A letter in German gvn_str = 'ß' print(gvn_str.isascii()) gvn_str = 'õ' print(gvn_str.isascii()) gvn_str = 'Å' print(gvn_str.isascii())
Output:
False False False
The isascii() method also verifies ASCII Unicode characters.
For Example:
gvn_str = '/u0000' # Null Unicode print(gvn_str.isascii()) gvn_str = '/u007f' # DEL Unicode print(gvn_str.isascii()) gvn_str = '/u0041' # A's Unicode print(gvn_str.isascii()) gvn_str = '/u0061' # a's Unicode print(gvn_str.isascii())
Output:
True True True True
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the string as user input using the input() function and store it in a variable.
- Apply isascii() method on the given string where if all of the characters are ASCII, the isascii() method returns True (a-z). Otherwise returns False.
- Store it in a variable.
- Print the result after applying isascii() method on the given string.
- The Exit of the Program.
Below is the implementation:
# Give the string as user input using the input() function and store it in a variable. gvn_str = input("Enter some random string = ") # Apply isascii() method on the given string where if all of the characters are # ASCII, the isascii() method returns True (a-z). Otherwise returns False. # Store it in a variable. rslt = gvn_str.isascii() # Print the result after applying isascii() method on the given string. print(rslt)
Output:
Enter some random string = hello@%btechgeeks670 True