python如果用户输入包含字符串 [英] python if user input contains string

查看:135
本文介绍了python如果用户输入包含字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常基本的问题。
我们有代码:

Very basic question. We have the code:

a = input("how old are you")

if a == string:
    do this

if a == integer (a != string):  
    do that

显然这不起作用。但是最简单的方法是什么呢。
预先感谢您提供任何答案。

Obviously it doesn't work that way. But what is the easiest way to do this. Thanks for any answers in advance.

我们也可以说:

if string in a:
    do this


推荐答案

你可以使用 str.isdigit str.isalpha

if a.isalpha():
   #do something
elif a.isdigit():
   #do something

帮助 str.isdigit

>>> print str.isdigit.__doc__
S.isdigit() -> bool

Return True if all characters in S are digits
and there is at least one character in S, False otherwise.

帮助 str.isalpha

>>> print str.isalpha.__doc__
S.isalpha() -> bool

Return True if all characters in S are alphabetic
and there is at least one character in S, False otherwise.

这篇关于python如果用户输入包含字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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