-1
  var1 = "0123456789"

I need to store this string into another string variables by breaking it into 3 parts like

  par1 = '0123'
  par2 = '4567'
  par3 ='89

Purpose: I have got a dataset I need to break the each record and store it in different variables.

Suneel Kumar
  • 1,650
  • 2
  • 21
  • 31

2 Answers2

0

If you know the length of each part, you can use string slicing and unpacking feature of python:

var1 = "0123456789"

par1, par2, par3 = var1[:4], var1[4:8], var1[8:]


>>> var1
'0123456789'
>>> par1
'0123'
>>> par2
'4567'
>>> par3
'89'
>>> 
Chen A.
  • 10,140
  • 3
  • 42
  • 61
0

It depends if you have constant 'slices' to do, for your example you can do:

par1 = var1[:4]
par2 = var1[4:8]
par3 = var1[8:]

play with this method, it works pretty well :)

A.Joly
  • 2,317
  • 2
  • 20
  • 25