1

Looking to split a simple string of 8 random digits:

myString = "08928192"

So that it will read as:

myStringX = "08"
myStringY = "92"
myStringA = "81"
myStringB = "92"

Looking for a method that is simple to follow and gives me these strings as shown. The number is generated from a random.rand_range() function earlier own. The need for this arises that I need a way to randomly define an objects map co-ordinates and two other properties( Most likely going to be health and size) for a resource-management style game I am working on. The rest of the code is easy enough, just this part stumped me.

Many Thanks ArchangelArchitect

2 Answers2

4
myString = "08928192"
n = 2

output = [myString[i:i+n] for i in range(0, len(myString), n)]

print(output)
e.doroskevic
  • 2,129
  • 18
  • 25
1

Just do:

import re

myString = "089281924" # 9 lenght
x, y, a, b, *tail = re.findall("\w{1,2}", myString)
print(x, y, a, b)
# 08 92 81 92
print(x, y, a, b, *tail)
# 08 92 81 92 4
Ali SAID OMAR
  • 6,404
  • 8
  • 39
  • 56