Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does raw_input().strip().split() in Python work in this code?

Hopefully, the community might explain this better to me. Below is the objective, I am trying to make sense of this code given the objective.

Objective: Initialize your list and read in the value of followed by lines of commands where each command will be of the types listed above. Iterate through each command in order and perform the corresponding operation on your list.

Sample input:

12
insert 0 5
insert 1 10
etc.

Sample output:

[5, 10]
etc.

The first line contains an integer, n, denoting the number of commands. Each line of the subsequent lines contains one of the commands described above.

Code:

n = int(raw_input().strip())

List = []
for number in range(n):
args = raw_input().strip().split(" ")
if args[0] == "append":
    List.append(int(args[1]))
elif args[0] == "insert":
    List.insert(int(args[1]), int(args[2]))

So this is my interpretation of the variable "args." You take the raw input from the user, then remove the white spaces from the raw input. Once that is removed, the split function put the string into a list.

If my raw input was "insert 0 5," wouldn't strip() turn it into "insert05" ?

like image 883
CTLearn Avatar asked Nov 14 '16 21:11

CTLearn


People also ask

What does Strip () split () do in Python?

Python string | strip() strip() is an inbuilt function in Python programming language that returns a copy of the string with both leading and trailing characters removed (based on the string argument passed). Syntax: string.

What is int raw_input () Strip ()) in Python?

n= int(raw_input().strip()) ---> int ( will try to convert to integer raw_input( this function is used take input from python command line ).strip( this function will remove all the spaces from input from left and right and not from in-between )

How do you use strip and split in Python?

There are three options for stripping characters from a string in Python, lstrip() , rstrip() and strip() . Each will return a copy of the string with characters removed, at from the beginning, the end or both beginning and end. If no arguments are given the default is to strip whitespace characters.

What does the Python raw_input () function do?

Python raw_input function is used to get the values from the user. We call this function to tell the program to stop and wait for the user to input the values. It is a built-in function.


2 Answers

In python you use a split(delimiter) method onto a string in order to get a list based in the delimiter that you specified (by default is the space character) and the strip() method removes the white spaces at the end and beginning of a string

So step by step the operations are:

raw_input()          #' insert 0 5     '
raw_input().strip()  #'insert 0 5'
raw_input().strip().split()  #['insert', '0', '5']

you can use split(';') by example if you want to convert strings delimited by semicolons 'insert;0;5'

like image 113
soloidx Avatar answered Nov 15 '22 11:11

soloidx


Let's take an example, you take raw input

string=' I am a coder '

While you take input in form of a string, at first, strip() consumes input i.e. string.strip() makes it

string='I am a coder' 

since spaces at the front and end are removed. Now, split() is used to split the stripped string into a list i.e.

string=['I', 'am', 'a', 'coder'] 
like image 30
Tek Raj Awasthi Avatar answered Nov 15 '22 11:11

Tek Raj Awasthi