python 字符串 变量_检查变量是否为字符串的Python程序

python 字符串 变量

Python | 检查变量是否为字符串 (Python | Check if a variable is a string)

To check whether a defined variable is a string type or not, we can use two functions which are Python library functions,

要检查定义的变量是否为字符串类型,我们可以使用两个函数,它们是Python库函数,

  1. Using isinstance()

    使用isinstance()

  2. Using type()

    使用type()

1)使用isinstance()函数检查变量是否为字符串 (1) Checking a variable is a string or not using isinstance() function)

isinstance() function accepts two parameters – 1) variable name (object) and 2) data type (class) and returns whether an object is an instance of a class or of a subclass thereof.

isinstance()函数接受两个参数– 1)变量名( 对象 )和2)数据类型( 类 ),并返回对象是类的实例还是其子类的实例。

Syntax:

句法:

    isinstance(obj, class_or_tuple)

Example:

例:

# variables
a = 100   # an integer variable
b = 10.23 # a float variable
c = 'A'   # a character variable 
d = 'Hello'   # a string variable
e = "Hello"   # a string variable 

# checking types
if isinstance(a, str):
  print("Variable \'a\' is a type of string.")
else:
  print("Variable \'a\' is not a type of string.")

if isinstance(b, str):
  print("Variable \'b\' is a type of string.")
else:
  print("Variable \'b\' is not a type of string.")

if isinstance(c, str):
  print("Variable \'c\' is a type of string.")
else:
  print("Variable \'c\' is not a type of string.")

if isinstance(d, str):
  print("Variable \'d\' is a type of string.")
else:
  print("Variable \'d\' is not a type of string.")

if isinstance(e, str):
  print("Variable \'e\' is a type of string.")
else:
  print("Variable \'e\' is not a type of string.")

Output

输出量

Variable 'a' is not a type of string.
Variable 'b' is not a type of string.
Variable 'c' is a type of string.
Variable 'd' is a type of string.
Variable 'e' is a type of string.

2)使用type()函数检查变量是否为字符串 (2) Checking a variable is string using type() function)

type() function accepts one parameter (others are optional), and returns its type.

type()函数接受一个参数(其他参数是可选的),并返回其类型。

Syntax:

句法:

    type(object)

Example:

例:

# variables
a = 100   # an integer variable
b = 10.23 # a float variable
c = 'A'   # a character variable 
d = 'Hello'   # a string variable
e = "Hello"   # a string variable 

# checking types
if type(a) == str:
  print("Variable \'a\' is a type of string.")
else:
  print("Variable \'a\' is not a type of string.")

if type(b) == str:
  print("Variable \'b\' is a type of string.")
else:
  print("Variable \'b\' is not a type of string.")

if type(c) == str:
  print("Variable \'c\' is a type of string.")
else:
  print("Variable \'c\' is not a type of string.")

if type(d) == str:
  print("Variable \'d\' is a type of string.")
else:
  print("Variable \'d\' is not a type of string.")

if type(e) == str:
  print("Variable \'e\' is a type of string.")
else:
  print("Variable \'e\' is not a type of string.")

Output

输出量

Variable 'a' is not a type of string.
Variable 'b' is not a type of string.
Variable 'c' is a type of string.
Variable 'd' is a type of string.
Variable 'e' is a type of string.


翻译自: https://www.includehelp.com/python/check-whether-a-variable-is-a-string-or-not.aspx

python 字符串 变量

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值