A Simple Guide to Detect Python String Contains Non-ASCII Characters – Python Tutorial

By | August 13, 2019

In many python applications, we have to process ascii python string, which means we should detect a python string contains non-ascii characters or not. In this tutorial, we will tell you how to do.

Before we start, you should know the scope of ascii characters.

An Introduction to ASCII (0 – 255)for Beginners – Python Tutorial

Import library

import re

Create a python string which contains non-ascii characters

str = u'这个是我的blog网站:https://www.tutorialexample.com'

Create a regular regression to detect

regexp = re.compile(r'[^\x00-\x7f]')

Notice: here we only process basic ascii characters, which are 0- 127.

If you want to process all ascii characters, which are 0 – 255, you can do like this:

regexp = re.compile(r'[^\x00-\xff]')

Meanwhile, you also can select to process the scope of ascii characters by ascii table.

Detect python string contains non-ascii or not

if regexp.search(str):
    print('non-ascill charracters are found')
else:
    print("all characters is ascii")

Output is:

non-ascill charracters are found

Notice: if you have find Non-UTF-8 error, you can refer to this tutorial.

Fix Python SyntaxError: Non-UTF-8 code starting with ‘xd5’ – Python Tutorial

Leave a Reply