-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
  • 112,593
  • 23
  • 157
  • 280
Weemo
  • 35
  • 1
  • 1
  • 2

2 Answers2

8

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

>>> my_str = '12x24'
>>> my_str.replace('x', ' x ')
'12 x 24'
Moinuddin Quadri
  • 43,657
  • 11
  • 92
  • 117
2

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

string.replace('x', ' x ')
Batman
  • 8,137
  • 7
  • 38
  • 75