-1

I need to add a space between X in a string. The program takes measurements in one field and I need to be able to separate integers from the "x" before doing the calculation.

For example: "12x24" should read "12 x 24"

martineau
  • 119,623
  • 25
  • 170
  • 301
Weemo
  • 35
  • 1
  • 1
  • 2

2 Answers2

11

Replace 'x' with '<space>x<space>' using str.replace() function as:

>>> my_str = '12x24'
>>> my_str.replace('x', ' x ')
'12 x 24'
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
2

Use the replace method to substitute ' x ' for 'x':

string.replace('x', ' x ')
Batman
  • 8,571
  • 7
  • 41
  • 80