0

I have this string:

number = "0123456789"

I want to create variables that are iterative having values from the string. For example,

I want a string like

a1 =1, 
a3=3,
... 
a8=8

Please advise. Thank You

4
  • why only a1, a3 and a8? why not other variables like a2, a4, a5, a6, a7 ,a9? Commented Jul 3, 2020 at 5:31
  • 1
    I think he was giving an example. @HarshaBiyani :) Commented Jul 3, 2020 at 5:32
  • ok,, then please modify your question.. Commented Jul 3, 2020 at 5:33
  • 1
    While this can be done - are you sure you really want to do it? Usually, you would use a list for this kind of thing (like a = [i for i in range(10)]). Commented Jul 3, 2020 at 5:35

5 Answers 5

1

There is ambiguity in question. I address the following part: 'I want to create variables that are iterative having values from the string.'

If you solve problem of creating variables dynamically you have created a new problem - how to access them dynamically. Therefore it's much better to use some built-in datastrucuture like list, tuple or dictionary.

Keeping this in mind answer to the question:

number = '0123456789'   

for num in number:                                  
    globals()[f'a{num}'] = int(num)

This code will create variables a0, a1...a9 with corresponding integer values.

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

2 Comments

Thank You for the assumptions. I am learning Python and hence the ambiguity. But what you provided as a solution, works for me. Thank You
I still strongly advise against creating variables this way. You are much better off with list/tuple/dictionary and accessing associated values with indices or keys.
0

To make an iterable object with indecies I do something like:

number = "0123456789"
new_number = ""
for i in number:
    if i != number[-1]:
        new_number += i + " "
    else:
        new_number += i
iterable = new_number.split(" ")

This could likely be made less messy if you are interested in that, but this does the trick. Note: the elements in the iterable will be strings.

Comments

0

this will do the trick

res = ', '.join(['a{}={}'.format(i,i) for i in number])

output

'a0=0, a1=1, a2=2, a3=3, a4=4, a5=5, a6=6, a7=7, a8=8, a9=9'

Comments

0

You can loop through each character in your variable number but you have to typecast it to string. And please note that number=0123456789 will error in Python. You need a string value. So typecasting the numeric value to string will solve it.

Example:

number = str(123456789)
for i in number:
    globals() ['a'+i] = int(i)

Then you can use the created variable as usual like this:

print(a1)
print(a9)

This will output:

1
9

I hope this answered your question.

Comments

0

I think what you are looking for is an iterator function - namely iter()

For example:

number = '0123456789'
iterator = iter(number)

Then, with next(), we can do something like this:

next(iterator)
'0'
next(iterator)
'1'

That is, you can use the next function to obtain the next value.

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.