如何在 Python 中检查字符串是否为空

PythonPythonBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在这个实验中,你将学习如何在 Python 中检查字符串是否为空。实验首先会介绍空字符串的概念,空字符串是指不包含任何字符的字符串,用单引号 ('') 或双引号 ("") 表示。你将使用单引号和双引号创建并打印空字符串,并验证它们的类型为 str

接着,实验会指导你使用 len() 函数来确定字符串的长度,并将字符串作为布尔值进行评估,以检查其是否为空。这些方法为你在 Python 代码中识别和处理空字符串提供了实用的方式。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/ControlFlowGroup(["Control Flow"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) python/BasicConceptsGroup -.-> python/variables_data_types("Variables and Data Types") python/BasicConceptsGroup -.-> python/strings("Strings") python/BasicConceptsGroup -.-> python/booleans("Booleans") python/ControlFlowGroup -.-> python/conditional_statements("Conditional Statements") python/FunctionsGroup -.-> python/build_in_functions("Build-in Functions") subgraph Lab Skills python/variables_data_types -.-> lab-559577{{"如何在 Python 中检查字符串是否为空"}} python/strings -.-> lab-559577{{"如何在 Python 中检查字符串是否为空"}} python/booleans -.-> lab-559577{{"如何在 Python 中检查字符串是否为空"}} python/conditional_statements -.-> lab-559577{{"如何在 Python 中检查字符串是否为空"}} python/build_in_functions -.-> lab-559577{{"如何在 Python 中检查字符串是否为空"}} end

了解空字符串

在这一步中,你将了解 Python 中的空字符串。空字符串就是不包含任何字符的字符串,用两个单引号 ('') 或两个双引号 ("") 表示。理解空字符串至关重要,因为它们常被用作变量的初始值,或用于表示数据缺失。

让我们先使用单引号创建一个空字符串:

  1. 在 LabEx 环境中打开 VS Code 编辑器。

  2. ~/project 目录下创建一个名为 empty_string.py 的新文件。

    ~/project/empty_string.py
  3. empty_string.py 文件中添加以下代码:

    ## Assign an empty string to a variable
    empty_string = ''
    
    ## Print the empty string
    print(empty_string)
    
    ## Check the type of the variable
    print(type(empty_string))
  4. 保存文件。

  5. 在 VS Code 中打开终端。

  6. 使用 python 命令运行脚本:

    python empty_string.py

    你应该会看到以下输出:

    <class 'str'>

    第一行是空的,因为我们打印的是一个空字符串。第二行确认了变量 empty_string 的类型是 str(字符串)。

现在,让我们使用双引号创建一个空字符串:

  1. 修改 empty_string.py 文件,使用双引号而非单引号:

    ## Assign an empty string to a variable
    empty_string = ""
    
    ## Print the empty string
    print(empty_string)
    
    ## Check the type of the variable
    print(type(empty_string))
  2. 保存文件。

  3. 再次运行脚本:

    python empty_string.py

    输出将是相同的:

    <class 'str'>

    这表明在 Python 中,单引号和双引号都可用于创建空字符串。

空字符串在各种场景中都很有用。例如,你可能会使用空字符串来初始化一个稍后将存储用户输入的变量,或者表示数据集中的缺失值。在接下来的步骤中,你将学习如何检查字符串的长度,以及空字符串如何作为布尔值进行评估。

使用 len() 检查长度

在这一步中,你将学习如何在 Python 中使用 len() 函数来确定字符串的长度。len() 函数会返回字符串中的字符数量。在处理字符串时,这是一项基本操作,因为它能让你验证输入、处理文本以及执行其他各种任务。

让我们先从检查空字符串的长度开始:

  1. 打开你在上一步中在 ~/project 目录下创建的 empty_string.py 文件。

  2. 修改代码,使用 len() 函数来检查空字符串的长度:

    ## Assign an empty string to a variable
    empty_string = ''
    
    ## Check the length of the empty string
    length = len(empty_string)
    
    ## Print the length
    print(length)
  3. 保存文件。

  4. 使用 python 命令运行脚本:

    python empty_string.py

    你应该会看到以下输出:

    0

    这证实了空字符串的长度为 0。

现在,让我们检查一个非空字符串的长度:

  1. 修改 empty_string.py 文件,将一个非空字符串赋值给 empty_string 变量:

    ## Assign a non-empty string to a variable
    empty_string = "Hello, LabEx!"
    
    ## Check the length of the string
    length = len(empty_string)
    
    ## Print the length
    print(length)
  2. 保存文件。

  3. 再次运行脚本:

    python empty_string.py

    你应该会看到以下输出:

    13

    字符串 "Hello, LabEx!" 的长度为 13,包括空格和感叹号。

len() 函数可以用于任何字符串,无论其内容如何。它是在 Python 中处理文本数据的一个通用工具。在下一步中,你将学习在 Python 中空字符串如何作为布尔值进行评估。

作为布尔值进行评估

在这一步中,你将学习在 Python 中空字符串如何作为布尔值进行评估。在 Python 里,空字符串被视为“假值”(falsy),这意味着当在布尔上下文(如 if 语句)中使用时,它会被评估为 False。相反,非空字符串被视为“真值”(truthy),会被评估为 True。理解这种行为对于编写能正确处理字符串值的条件语句至关重要。

让我们先在 if 语句中测试一个空字符串:

  1. 打开你在前面步骤中于 ~/project 目录下创建的 empty_string.py 文件。

  2. 修改代码,加入一个 if 语句来检查空字符串的布尔值:

    ## Assign an empty string to a variable
    empty_string = ''
    
    ## Check if the empty string is truthy or falsy
    if empty_string:
        print("The string is truthy")
    else:
        print("The string is falsy")
  3. 保存文件。

  4. 使用 python 命令运行脚本:

    python empty_string.py

    你应该会看到以下输出:

    The string is falsy

    这证实了空字符串在布尔上下文中会被评估为 False

现在,让我们在 if 语句中测试一个非空字符串:

  1. 修改 empty_string.py 文件,将一个非空字符串赋值给 empty_string 变量:

    ## Assign a non-empty string to a variable
    empty_string = "Hello, LabEx!"
    
    ## Check if the non-empty string is truthy or falsy
    if empty_string:
        print("The string is truthy")
    else:
        print("The string is falsy")
  2. 保存文件。

  3. 再次运行脚本:

    python empty_string.py

    你应该会看到以下输出:

    The string is truthy

    这表明非空字符串在布尔上下文中会被评估为 True

这种特性对于检查字符串是否已被赋值或是否包含有意义的数据很有用。你可以利用这些知识编写更健壮、更灵活的 Python 代码。

总结

在这个实验中,你学习了 Python 中的空字符串,即不包含任何字符的字符串,用单引号 ('') 或双引号 ("") 表示。你使用单引号和双引号创建了空字符串,并将其赋值给变量,还使用 type() 函数验证了其类型为 str

本实验展示了在 Python 中,单引号和双引号在创建空字符串时可以互换使用。理解这一点至关重要,因为空字符串常用于初始化变量或表示数据缺失。