Python string isascii method example:
isascii method was introduced in Python 3.7. Using this method, we can check if a string contains all ASCII characters. This method can be used to check all characters are ASCII or not without iterating through the characters one by one and test them.
In this post, I will show you how to use isascii with example.
Definition of isascii():
isascii is defined as below:
str.isascii()
This method returns a boolean value. It is True if the string is empty or all characters in the string are ASCII. Else it returns False.
Example of isascii:
Let’s take a look at the below example:
str_arr = ['a', '', 'abc', 'ab♥c123', '123♥', 'abc#$%', '♥♥♥♥♥']
for item in str_arr:
print('{} isascii : {}'.format(item, item.isascii()))
Here, we are checking isascii for all of the strings in str_arr. It gives the below output:
a isascii : True
isascii : True
abc isascii : True
ab♥c123 isascii : False
123♥ isascii : False
abc#$% isascii : True
♥♥♥♥♥ isascii : False
You might also like:
- Python program to add items to a list without using append()
- Python program to capitalize all characters of a string without using inbuilt functions
- Python program to get the string between two substrings
- Python string join() method explanation with example
- How to copy string in python
- Python check if a string is alphanumeric