0

I have a string that reads "blah blah @~@ blah blah @~@ blah blah @~@"

How could I make it so that it creates a variable(string) for each '@~@' that feature in the string?

In this example I would end up with three variables - split1, split2 and split3

2
  • 2
    Can you give a Minimal, Complete, and Verifiable example about what you want to do? or the code that you have tried so far? Commented May 27, 2015 at 9:46
  • 2
    string.split('@~@') Commented May 27, 2015 at 9:49

2 Answers 2

2

try something like this

str = "blah blah @~@ blah blah @~@ blah blah @~@";
split = str.split('@~@');

then you can access the individual values as:

print(split[0],split[1],split[2])

Output:

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

2 Comments

A loop to print it out would be better.
This works great! I've used .split before and don't know why it just flew over my head! Is it it possible I could get it to add 1 to an integer for every '@~@' it reads in the string?
0

As Joao said, and you also suggested in your question. You can use str.split to create a list of names. Rather than creating variable names based on these it is much more "pythonic" to create a dictionary based on these. i.e.

str = "name1 @~@ name2 @~@ name3";

x = str.split('@~@');

print x



d = dict((x[n],0) for n in xrange(0, len(x), 1))

print d

You could use exec but I would strongly advise against it.

1 Comment

This won't work in python 3. You need parenthesis in the call to print.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.