user2272942
user2272942

Reputation: 63

Replacing spaces with a letter in python

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

Upvotes: 0

Views: 344

Answers (4)

ronak
ronak

Reputation: 1778

As an alternative you could use the regular expression module

import re

In [9]: re.sub(' ', 'x', text)
Out[9]: 'howxisxyourxdayx?'

Upvotes: 0

Matt
Matt

Reputation: 1198

You can use replace()

text.replace(' ', 'x')

Upvotes: 2

jamylak
jamylak

Reputation: 133514

>>> text = 'how is your day ?'
>>> text.replace(' ', 'x')
'howxisxyourxdayx?'

Upvotes: 0

squiguy
squiguy

Reputation: 33360

Try using replace:

string.replace(' ', 'x')

Upvotes: 0

Related Questions