【问题标题】:Does python see an empty string as uppercase or lowercase?python 是否将空字符串视为大写或小写?
【发布时间】:2020-04-29 02:30:45
【问题描述】:

很尴尬,我们有很多关于以下所有标签的问题: ,但没有问题询问python是否将空字符串视为大写或小写。那么,python 是把空字符串看成大写还是小写呢?

【问题讨论】:

  • 为什么尴尬?

标签: uppercase python lowercase empty-string python string uppercase lowercase


【解决方案1】:

如果你运行这个:

empty = ""

if empty.islower():
    print("The empty string is lowercase")
elif empty.isupper():
    print("The empty string is uppercase")
else:
    print("The empty string is neither!")

你应该得到:

The empty string is neither!

这是因为isupper 在继续检查其大小写之前首先检查是否有多个字符(look here):

Python isupper 是 Python 字符串方法之一,用于检查给定字符串是否至少包含一个字符,并且该字符是否为大写。如果是大写,则 Python isupper 函数返回 True;否则返回 False。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-18
    • 2016-01-22
    • 2020-09-13
    • 2018-09-07
    • 2016-12-11
    • 2012-01-03
    • 2021-02-03
    • 1970-01-01
    相关资源
    最近更新 更多