Python 3.2中的外来字符

Python 3.2中的外来字符,python,string,python-3.x,Python,String,Python 3.x,我正在用Python 3.2编写一个程序,它需要能够检测字符串是否包含外来字符“å,ä,ö”。我已经编写了以下代码: # This Python file uses the following encoding: utf-8 name = input('Give your name: ') if ('å' in name) or ('ä' in name) or ('ö' in name): print('Foreign characters found') 但是,如果我输入例如“Å

我正在用Python 3.2编写一个程序,它需要能够检测字符串是否包含外来字符“å,ä,ö”。我已经编写了以下代码:

# This Python file uses the following encoding: utf-8

name = input('Give your name: ')

if ('å' in name) or ('ä' in name) or ('ö' in name):
   print('Foreign characters found')

但是,如果我输入例如“Åke”,程序仍然不会执行print命令。你知道为什么会这样吗?或者您对如何检测给定字符串中的字符“å,ä,ö”还有其他想法吗?

您现在只是检查这三个字符,虽然这并不能回答您的问题,但这可能是检查外来字符的更好方法:

try:
    name.encode('ascii')
except UnicodeError:
    print('Foreign characters found')

注意:在python 2.7中进行了测试,但我认为它也应该在3.2中工作。

您现在只检查这三个字符,虽然这并不能回答您的问题,但这可能是检查外来字符的更好方法:

try:
    name.encode('ascii')
except UnicodeError:
    print('Foreign characters found')

注意:在python 2.7中测试,但我认为它也应该在3.2中工作。

你知道“å”和“Å”是两个不同的字符吗?使用
name=input('Give your name:')。lower()
你的终端使用UTF-8吗?@SvenMarnach:我想我可以通过将所有字符都改为小写来解决这个问题,正如布莱斯建议的那样。@chepner:我必须承认我真的不知道,我对这一点还很陌生。我使用的是MacBookPro。你知道“å”和“Å”是两个不同的字符吗?使用
name=input('Give your name:')。lower()
你的终端使用UTF-8吗?@SvenMarnach:我想我可以通过将所有字符都改成小写来解决这个问题,就像BrtH建议的那样。@chepner:我必须承认我真的不知道,我对这个很陌生。我正在使用MacBookPro。
除了UnicodeError:
是等效的-UnicodeEncodeError和UnicodeDecodeError是UnicodeError的子类。@ThomasK,好的,我相信你,我不是这样的unicode专家:P
除了unicode错误:
是等效的-unicode错误和unicode错误是unicode错误的子类。@ThomasK,好吧,我相信你,我不是这样的unicode专家:P