Re-formatting user input with spaces

2019-09-30 08:27发布

问题:

I'm using an input function where I want to convert any spaces in the input to +'s. So for example, if the user inputs iphone 7 black, I want to convert this to iphone+7+black.

回答1:

Just use replace:

>>> text = raw_input('Enter text: ')
Enter text: iphone 7 black
>>> text.replace(' ', '+')
'iphone+7+black'


标签: python input