Remember
isascii() tests whether every character is ASCII. An empty string also satisfies that condition.
isascii() tests whether every character is ASCII. An empty string also satisfies that condition.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = 'tea'
result = text.isascii()
print(result)Top-level scope
'tea'Object 2 · boolTrueisascii() tests whether every character is ASCII. An empty string also satisfies that condition.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = 'tea'
result = text.isascii()
print(result)
|
v
DISPLAYED RESULT
True
FINAL BINDINGS
text --> object 1 (str)
result --> object 2 (bool)
object 1: 'tea'
object 2: TrueChoose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = 'tea'
result = text.isascii()
print(result)Top-level scope
'tea'Object 2 · boolTrueThe example has finished. Compare the final output with the values and references above.
True
What do you expect this program to print?
text = 'tea'
result = text.isascii()
print(result)isascii() MethodThe isascii() method in Python checks whether all the characters in a string are ASCII characters (i.e., in the range 0 to 127).
It returns True if all characters are ASCII, otherwise False. This method is available from Python 3.7 and later.
string.isascii()
True – If all characters in the string are ASCII.False – If at least one character is not an ASCII character.text = "Hello123"
print(text.isascii())
True
text = "Café"
print(text.isascii())
False
The character é is not an ASCII character.
text = ""
print(text.isascii())
True
An empty string is considered ASCII.
isascii() checks if a string contains only letters or digits. It doesn’t—it allows symbols, punctuation, and whitespace too, as long as they’re ASCII.AttributeError.If you’re asked to filter out non-ASCII characters from a string, isascii() can help quickly detect such cases or be used in list comprehensions to clean strings.
string.isascii() checks if all characters are in the ASCII range.True for ASCII-only strings, False otherwise.True.Write a program that checks if user input contains only ASCII characters.
text = input("Enter some text: ")
if text.isascii():
print("All characters are ASCII.")
else:
print("Non-ASCII characters found!")
Connect what you noticed in the example with the idea below.
isascii() tests whether every character is ASCII. An empty string also satisfies that condition.
isascii() returns False for the empty string.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.