isalpha python 函数不会考虑空格 [英] isalpha python function won't consider spaces

查看:68
本文介绍了isalpha python 函数不会考虑空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以下面的代码接受一个输入并确保输入由字母而不是数字组成.如果输入包含空格,我将如何使其也打印原始

So the code below takes an input and makes sure the input consists of letters and not numbers. How would i make it also print orginal if the input contains a space

original = raw_input("Type the name of the application: ")

if original.isalpha() and len(original) > 0:
    print original
else:
    print "empty"

尝试了此代码,但在输入也是数字时也能工作.

tried this code but worked when the input was a number too.

original = raw_input("Type the word you want to change: ")

if original.isalpha() or len(original) > 0:
    print original
else:
    print "empty"

推荐答案

看起来字符串就是这样工作的.

It looks like that's just how string works.

两个选项:

if all(x.isalpha() or x.isspace() for x in original):

(根据以下InspectorG4dget的建议修改)

(modified on inspectorG4dget's recommendation below)

original.replace(' ','').isalpha()

应该可以.

这篇关于isalpha python 函数不会考虑空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆