pythonstring

Getting the Second Word in a Python 3 String


I am trying to make a terminal in python, in which I can enter commands and have the program execute certain actions. I want to assign a variable to the string I get from an input like so :

cmd = input("Enter your command:")

Lets say I'm trying to make a Logging Command, where I would type out log primary {text} (which would store a string in a primary dictionary) or log secondary {text} (which would store the string in a secondary dictionary).

I want to be able to assign a variable to the second word in the command string. When I type log primary, I want to assign a variable to the word primary only.

In another words, how do I assign a variable to the second and third words of a string?


Solution

  • var = cmd.split(" ")[1]
    

    Here we are just splitting the string into an list and the criteria is " " (space) then its just indexing through the list.

    Thats it !