0

Im trying to Replace the spaces in a string i have with a "x" its for a function and im not sure of the best way to go about this ?

for example

how is your day ?

i would like this to be howxisxyourxdayx?

thanks for your help

jamylak
  • 128,818
  • 30
  • 231
  • 230
user2272942
  • 63
  • 1
  • 1
  • 8

4 Answers4

2

You can use replace()

text.replace(' ', 'x')
Matt
  • 1,148
  • 10
  • 15
0

Try using replace:

string.replace(' ', 'x')
squiguy
  • 32,370
  • 6
  • 56
  • 63
  • would you know how to reverse a string for example red turning into der? – user2272942 Apr 12 '13 at 04:48
  • @user2272942 Ask single questions here, don't use the comments for unrelated things, anyway for that `string[::-1]`. Please read the python tutorial – jamylak Apr 12 '13 at 04:49
  • @user2272942 Sure, try looking here: http://stackoverflow.com/questions/931092/reverse-a-string-in-python – squiguy Apr 12 '13 at 04:49
0
>>> text = 'how is your day ?'
>>> text.replace(' ', 'x')
'howxisxyourxdayx?'
jamylak
  • 128,818
  • 30
  • 231
  • 230
0

As an alternative you could use the regular expression module

import re

In [9]: re.sub(' ', 'x', text)
Out[9]: 'howxisxyourxdayx?'
ronak
  • 1,770
  • 3
  • 20
  • 34