Is it possible to split a string in python and assign each piece split off to a variable to be used later? I would like to be able to split by length if possible, but im not sure how it would work using len().
i tried this but its not getting me what i needed:
x = 'this is a string'
x.split(' ', 1)
print x
result:
['this']
i want to result to something like this:
a = 'this'
b = 'is'
c = 'a'
d = 'string'
If you'd like to access a string 3 characters at a time, you're going to need to use slicing.
You can get a list of the 3-character long pieces of the string using a list comprehension like this:
>>> x = 'this is a string'
>>> step = 3
>>> [x[i:i+step] for i in range(0, len(x), step)]
['thi', 's i', 's a', ' st', 'rin', 'g']
>>> step = 5
>>> [x[i:i+step] for i in range(0, len(x), step)]
['this ', 'is a ', 'strin', 'g']
The important bit is:
[x[i:i+step] for i in range(0, len(x), step)]
range(0, len(x), step)
gets us the indices of the start of each step
-character slice. for i in
will iterate over these indices. x[i:i+step]
gets the slice of x
that starts at the index i
and is step
characters long.
If you know that you will get exactly four pieces every time, 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 pieces, then Python will give you a ValueError
trying to unpack this list. Because of this, I would consider this technique very brittle, and would not use it.
You can simply do
x_pieces = [x[i:i+step] for i in range(0, len(x), step)]
Now, where 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 much more flexibility.
a couple of alternatives
I don't normally lean towards regular expressions, but to chunk a string, it's not too bad to use:
>>> s = 'this is a string'
>>> re.findall('.{1,3}', s)
['thi', 's i', 's a', ' st', 'rin', 'g']
And overkill
>>> t = StringIO(s)
>>> list(iter(lambda: t.read(3), ''))
['thi', 's i', 's a', ' st', 'rin', 'g']
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', 's i', 's a', ' st', 'rin', 'g']
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 in one line... here you can also see how to use len()
the while loop will print each line untill the counter has reached the maximum length
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 keep each number, keep them in a 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
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 this way:
if __name__ == '__main__':
x="my name is good"
tst(x)
This will produce the exact output you wanted under the constraint that the string has less than 27 words. You can always use generators in case you run out of keys to represent the chunks.
x = 'this is a string'
chunks = x.split(' ')
key = 'a'
for chunk in chunks:
print key + " = " + chunk
key = chr(ord(key) + 1)