Python section and separation

I want to split a string into two words like word1 word2, using split and partition and print (using for) words separately, for example:

Partition: word1 word2 Split: word1 word2 

This is my code:

 print("Hello World") name = raw_input("Type your name: ") train = 1,2 train1 = 1,2 print("Separation with partition: ") for i in train1: print name.partition(" ") print("Separation with split: ") for i in train1: print name.split(" ") 

It happens:

 Separation with partition: ('word1', ' ', 'word2') ('word1', ' ', 'word2') Separation with split: ['word1', 'word2'] ['word1', 'word2'] 
+9
source share
3 answers

A command of type name.split() returns a list. You can consider iterating over this list:

 for i in name.split(" "): print i 

Because the thing you wrote, namely

 for i in train: print name.split(" ") 

will execute the command print name.split(" ") twice (once for the value i=1 and again for i=2 ). And twice, it prints the whole result:

 ['word1', 'word2'] ['word1', 'word2'] 

A similar situation occurs with partition - except that it returns an item that you also split. Therefore, in this case, you may want to do

 print name.partition(" ")[0:3:2] 

to return elements 0 and 2 . Alternatively, you can do

 train = (0, 2,) for i in train: print name.partition(" ")[i] 

To print elements 0 and 2 in two consecutive passes through the loop. Note that this last code is more inefficient as it calculates the section twice. If you care, you can write

 train = (0,2,) part = name.partition(" ") for i in train: print part[i] 
+15
source

str.partition returns a tuple of three elements. The line before the section line, the section itself, and the rest of the line. Therefore, it should be used as

 first, middle, rest = name.partition(" ") print first, rest 

To use str.split you can just print split lines like this

 print name.split(" ") 

But, when you call it like this if the line contains more than one space, you will get more than two elements. for instance

 name = "word1 word2 word3" print name.split(" ") # ['word1', 'word2', 'word3'] 

If you want to split only once, you can specify the number of times that you want to split, as a second parameter, for example

 name = "word1 word2 word3" print name.split(" ", 1) # ['word1', 'word2 word3'] 

But, if you are trying to break based on the space characters, you do not need to pass " " . You can just do

 name = "word1 word2 word3" print name.split() # ['word1', 'word2', 'word3'] 

If you want to limit the number of sections,

 name = "word1 word2 word3" print name.split(None, 1) # ['word1', 'word2 word3'] 

Note: Using None in split or not specifying any parameters is what happens.

Quote from shared documentation

If sep is not specified or None, another separation algorithm is applied: consecutive space runs are treated as single delimiters, and the result will not contain empty lines at the beginning or end if the line has leading or trailing white space. As a result, splitting an empty string or a string consisting of just whitespace with a separator None returns [].

So you can change your program as follows

 print "Partition:" first, middle, rest = name.partition(" ") for current_string in (first, rest): print current_string print "Split:" for current_string in name.split(" "): print current_string 

Or you can use the str.join method similar to this

 print "Partition:" first, middle, rest = name.partition(" ") print "\n".join((first, rest)) print "Split:" print "\n".join(name.split()) 
+20
source

The partition () method breaks the line when the separator first appears and returns a tuple containing the part before the separator, the separator, and the part after the separator.

string = "Deepak is a good person, and Preity is not a good person." // separator 'is' found for the first time print (string.partition ('is'))

Output:

("Deepak," "is," "a good man, and Preity is not a good man.")

And in split, string = "Deepak is a good person, and Preity is not a good person." // the separator 'is' is in each case print (string.partition ('is'))

Output:

(Deepak, Eat, Good Man and Preity, Eat, Not A Good Man.)

Simply put, split will break the line whenever this argument appears, while split will break the line only when this argument first appears and will return a 3-tuple with the specified argument as the average value.

0
source

All Articles