How to take string with space in match case in Python 3.10

Viewed 192

I've a code in python 3.10 with a match - case structure. It's a terminal's apps, with command. How to take all the text after the "cesar" in one variable? Because space break the commands.

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

Use * to match a sublist, just like you would in a function parameter list to get all the remaining arguments.

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

For "all the text after cesar in one variable", if you mean as one str variable, use maxsplit=1; otherwise, Barmar's answer is probably what you want:

def cesar(mot):
    print(f'{mot=}')

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

Output:

->cesar one two three
mot='one two three'
Related