Python - split a string into smaller chunks and assign a variable

Is it possible to split a string in python and assign each part to be separated by a variable that will be used later? I would like to be able to split by length if possible, but I'm not sure how this will work with len ().

I tried this, but did not understand what I needed:

x = 'this is a string' x.split(' ', 1) print x 

Result: ['this']

I want to lead to something like this:

 a = 'this' b = 'is' c = 'a' d = 'string' 
+8
python
source share
8 answers

If you want to access a string 3 characters at a time, you will need slicing .

You can get a list of 3-character long snippets of a string using the following list comprehension:

 >>> x = 'this is a string' >>> step = 3 >>> [x[i:i+step] for i in range(0, len(x), step)] ['thi', i', a', ' st', 'rin', 'g'] >>> step = 5 >>> [x[i:i+step] for i in range(0, len(x), step)] ['this ', 'is a ', 'strin', 'g'] 

Important bit:

 [x[i:i+step] for i in range(0, len(x), step)] 

range(0, len(x), step) gets the start indices of each fragment step -character. for i in will iterate over these indices. x[i:i+step] gets a slice x starting with index i and length step .

If you know that each time you get exactly four pieces, then you can do:

 a, b, c, d = [x[i:i+step] for i in range(0, len(x), step)] 

This will happen if 3 * step < len(x) <= 4 * step .

If you don't have exactly four parts, then Python will give you a ValueError , trying to unpack this list. Because of this, I would find this method very fragile and not use it.

You can just do

 x_pieces = [x[i:i+step] for i in range(0, len(x), step)] 

Now that you used to access a , you can access x_pieces[0] . For b you can use x_pieces[1] and so on. This allows you to greatly increase flexibility.

+9
source share

You can use unpack

 a,b,c,d=x.split(' '); 
+5
source share

several alternatives

I usually do not tend to regexes, but in order to cut the string, this is not so bad:

 >>> s = 'this is a string' >>> re.findall('.{1,3}', s) ['thi', i', a', ' st', 'rin', 'g'] 

And overkill

 >>> t = StringIO(s) >>> list(iter(lambda: t.read(3), '')) ['thi', i', a', ' st', 'rin', 'g'] 
+5
source share

you can try something like this:

 In [77]: x = 'this is a string' In [78]: a,b,c,d=[[y] for y in x.split()] In [79]: a Out[79]: ['this'] In [80]: b Out[80]: ['is'] In [81]: c Out[81]: ['a'] In [82]: d Out[82]: ['string'] 

using itertools.islice() :

 In [144]: s = 'this is a string' In [145]: lenn=len(s)//3 if len(s)%3==0 else (len(s)//3)+1 In [146]: it=iter(s) In [147]: ["".join(islice(it,3)) for _ in range(lenn)] Out[147]: ['thi', i', a', ' st', 'rin', 'g'] 
+4
source share
 x = 'this is a string' splitted = x.split() count = 0 while count <= len(splitted) -1: print splitted[count] count = count + 1 

This will print each part on one line ... here you can also see how to use len()

while loop will print each line until the counter reaches maximum length

+1
source share
 x, i = 'this is a string', 0 #assigning two variables at once while i <= len(x): y = x[i: i + 3] print y i += 3 #i = i + 3 

This includes space characters ('').

If you want to save each number, save them in the list:

 x, my_list, i = 'this is a string', [], 0 while i <= len(x): y = x[i : i + 3] my_list.append(y) i += 3 
+1
source share
  def tst(sentence): print sentence bn=sentence.split(" "); i=0 for i in range(0,len(bn)): a= bn[i] i=i+1 print a 

Test it like this:

  if __name__ == '__main__': x="my name is good" tst(x) 
0
source share

This will give the exact result you would like to get when limiting that the line has less than 27 words. You can always use generators in case you run out of keys to represent pieces.

 x = 'this is a string' chunks = x.split(' ') key = 'a' for chunk in chunks: print key + " = " + chunk key = chr(ord(key) + 1) 
0
source share

All Articles