本文介绍了如何检查Python中的字符串是否为ASCII?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查字符串是否为ASCII.

I want to I check whether a string is in ASCII or not.

我知道ord(),但是当我尝试ord('é')时,我有TypeError: ord() expected a character, but string of length 2 found.我了解这是由我构建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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 21:29