ajkey94
ajkey94

Reputation: 431

How do I replace multiple spaces with just one character?

Here's my code so far:

input1 = input("Please enter a string: ")
newstring = input1.replace(' ','_')
print(newstring)

So if I put in my input as:

I want only    one     underscore.

It currently shows up as:

I_want_only_____one______underscore.

But I want it to show up like this:

I_want_only_one_underscore.

Upvotes: 12

Views: 20795

Answers (3)

funk
funk

Reputation: 2287

First approach (doesn't work)

>>> a = '213         45435             fdgdu'
>>> a
'213         45435                            fdgdu                              '
>>> b = ' '.join( a.split() )
>>> b
'213 45435 fdgdu'

As you can see the variable a contains a lot of spaces between the "useful" sub-strings. The combination of the split() function without arguments and the join() function cleans up the initial string from the multiple white spaces.

The previous technique fails when the initial string contains special characters such as '\n':

>>> a = '213\n         45435\n             fdgdu\n '
>>> b = ' '.join( a.split() )
>>> b
'213 45435 fdgdu'   (the new line characters have been lost :( )

In order to correct this we can use the following (more complex) solution.

Second approach (works)

>>> a = '213\n         45435\n             fdgdu\n '
>>> tmp = a.split( ' ' )
>>> tmp
['213\n', '', '', '', '', '', '', '', '', '45435\n', '', '', '', '', '', '', '', '', '', '', '', '', 'fdgdu\n', '']
>>> while '' in tmp: tmp.remove( '' )
... 
>>> tmp
['213\n', '45435\n', 'fdgdu\n']
>>> b = ' '.join( tmp )
>>> b
'213\n 45435\n fdgdu\n'

Third approach (works)

This approach is a little bit more pythonic in my eyes. Check it:

>>> a = '213\n         45435\n             fdgdu\n '
>>> b = ' '.join( filter( len, a.split( ' ' ) ) )
>>> b
'213\n 45435\n fdgdu\n'

Upvotes: 5

Henry Keiter
Henry Keiter

Reputation: 17168

Dirty way:

newstring = '_'.join(input1.split())

Nicer way (more configurable):

import re
newstring = re.sub('\s+', '_', input1)

Extra Super Dirty way using the replace function:

def replace_and_shrink(t):
    '''For when you absolutely, positively hate the normal ways to do this.'''
    t = t.replace(' ', '_')
    if '__' not in t:
        return t
    t = t.replace('__', '_')
    return replace_and_shrink(t)

Upvotes: 6

John La Rooy
John La Rooy

Reputation: 304137

This pattern will replace any groups of whitespace with a single underscore

newstring = '_'.join(input1.split())

If you only want to replace spaces (not tab/newline/linefeed etc.) it's probably easier to use a regex

import re
newstring = re.sub(' +', '_', input1)

Upvotes: 34

Related Questions