如何检查python中的空白输入 [英] How to check for white space input in python

查看:38
本文介绍了如何检查python中的空白输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程和python的新手.我正在编写脚本,如果客户键入空格,则要退出脚本.问题是我该怎么办?这是我的尝试,但我认为是错误的

I'm very new to programming and python. I'm writing a script and I want to exit the script if the customer type a white space. questions is how do I do it right? This is my attempt but I think is wrong

例如

userType = raw_input('Please enter the phrase to look: ')
userType = userType.strip()

line = inf.readline()
while (userType == raw_input)
    print "userType\n"

    if (userType == "")
        print "invalid entry, the program will terminate"
        # some code to close the app

推荐答案

我知道这很旧,但是将来可能会对某人有所帮助.我想出了如何用正则表达式来做到这一点.这是我的代码:

I know this is old, but this may help someone in the future. I figured out how to do this with regex. Here is my code:

import re

command = raw_input("Enter command :")

if re.search(r'[\s]', command):
    print "No spaces please."
else:
    print "Do your thing!"

这篇关于如何检查python中的空白输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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