python - 如何在 Python 3.10 的匹配大小写中使用带空格的字符串

标签 python match python-3.10

我在 python 3.10 中有一段代码,带有匹配 - 大小写结构。它是终端的应用程序,带有命令。如何在一个变量中获取“cesar”之后的所有文本?因为空格会破坏命令。

user= input("->")
match user.split():
    case["cesar" ,mot]:
        cesar(mot)
    case _:
        print("your answer is incorrect")

最佳答案

使用 * 匹配子列表,就像在函数参数列表中获取所有剩余参数一样。

match user.split():
    case ["cesar", *mot]:
        cesar(mot)
    case _:
        print("your answer is incorrect")

关于python - 如何在 Python 3.10 的匹配大小写中使用带空格的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69774127/

相关文章:

javascript - Websocket 是否发送和接收完整消息?

android - 通过 XML 设置 AdMob 横幅以匹配父级宽度

php - 在 MATCH AGAINST 中使用 CONCAT

android - 根据字符串列选择不同的第一个字符

python regex lookbehind删除字符串中的_sublabel1,如 "__label__label1_sublabel1"

python - 为什么 Python 将列表作为元组进行匹配?

type-hinting - 如何正确键入提示类装饰器?

python - 设置 DateTimeField 的最小值

python - 哪个 Python IDE 具有 Visual Studio 功能?

python - 使用 pandas pd.cut 生成带有 statsmodels 的分类变量