如何检查 Python 中的字符串是否为 ASCII? [英] How to check if a string in Python is in ASCII?
本文介绍了如何检查 Python 中的字符串是否为 ASCII?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想检查一个字符串是否为 ASCII.
I want to I check whether a string is in ASCII or not.
我知道 ord()
,但是当我尝试 ord('é')
时,我有 TypeError: ord() 需要一个字符,但是找到了长度为 2 的字符串
.我知道这是由我构建 Python 的方式引起的(如 ord()
的文档).
I am aware of ord()
, however when I try ord('é')
, I have TypeError: ord() expected a character, but string of length 2 found
. I understood it is caused by the way I built Python (as explained in ord()
's documentation).
还有其他方法可以检查吗?
Is there another way to check?
推荐答案
def is_ascii(s):
return all(ord(c) < 128 for c in s)
这篇关于如何检查 Python 中的字符串是否为 ASCII?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文