如何检查用户输入是否是python 3中的字符串 [英] how to check if the user input is a string in python 3

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

问题描述

我写了下面的代码:

try:
    nums=input("Write a name:")
    print (nums)
except ValueError:
    print ("You didn't type a name")

问题是即使用户输入一个数字,程序也会打印它`

The problem is that even the user enter a number the program prints it `

推荐答案

您可以使用函数 yourstring.isalpha() 如果字符串中的所有字符都来自字母表,它将返回 true.所以对于你的例子:

You can use the function yourstring.isalpha() it will return true if all characters in the string are from the alphabet. So for your example:

nums = input("write a name:")
if(not nums.isalpha()):
    print("you did not write a name!")
    return
print(nums)

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

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