10

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'
5
  • 1
    Why do you want your results stored in single item lists? Commented Dec 6, 2012 at 18:19
  • print x won't print what you say it prints. The result of the split is never assigned back to x. Commented Dec 6, 2012 at 18:20
  • That would be a very weird thing to do, what will use those variables later and how will it know how many there are. On the other hand you could use something like a dict to store each item with a certain key, although you'd still have problems if you were to be working on really long strings. But still, why? Commented Dec 6, 2012 at 18:24
  • i need to split out a large chunk of text to assign to various fields, ill edit to show what i really wanted, just needed strings Commented Dec 6, 2012 at 18:46
  • @DanielFigueroa the purpose is to take a paragraph and split out each line to deliver to a set of text fields on an XML form. Im limited by what im working in and i can only do so much. I cannot edit how the program works but only put some code inside it to work with. hence me pulling this text, splitting it up, then assigning to various fields on the XML form. Commented Dec 6, 2012 at 20:06

8 Answers 8

11

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.

Sign up to request clarification or add additional context in comments.

4 Comments

this seems to be closest to what i really want to do, but how could i assign the results of each range to a variable, could i do this? a, b, c, d = [x[i:i+step] for i in range(0, len(x), step)]
If you knew that you would always get exactly four results from separating the list, then you could do exactly that. But if you don't have exactly four arguments, this is going to throw an exception.
thanks...tried it and figured that out the hard way lol, so enumeration is the way to go then? thanks for the extra explanations as well, really helps for learning
Yeah, I think a list (enumeration) is a more natural format for expressing this data, since you don't know ahead of time how many pieces there will be. And I'm glad my explanations help. :-)
6

You can use unpacking

a,b,c,d=x.split(' ');

1 Comment

Since the split method uses whitespace by default, the function call you used could be condensed into a,b,c,d=x.split(). The semi-colon is not necessary.
5

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']

Comments

4

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']

4 Comments

how would you do it if you wanted to split by len()? lets say by 3 characters at a time?
I don't understand, what do you mean by three characters at a time?
yes @jonclements is correct, im trying to get a len() function to pull out a specific length of characters in the string and then assign each chunk to a variable, much like the example here
@AshwiniChaudhary thanks, its almost what im going for, just need to assign variable to that list somehow so i can extract each chunk to use later
1
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

Comments

1
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

Comments

0
 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)

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.