0

Basically, I'm trying to do a code in Python where a user inputs a sentence. However, I need my code to remove ALL whitespaces (e.g. tabs, space, index, etc.) and print it out.

This is what I have so far:

def output_without_whitespace(text):
  newText = text.split("")

print('String with no whitespaces: '.join(newText))

I'm clear that I'm doing a lot wrong here and I'm missing plenty, but, I haven't been able to thoroughly go over splitting and joining strings yet, so it'd be great if someone explained it to me.

This is the whole code that I have so far:

text = input(str('Enter a sentence: '))
print(f'You entered: {text}')

def get_num_of_characters(text):
 result = 0
 for char in text:
     result += 1
 return result
print('Number of characters: ', get_num_of_characters(text))

def output_without_whitespace(text):
    newtext = "".join(text.split())

print(f'String without whitespaces: {newtext}')

I FIGURED OUT MY PROBLEM! I realize that in this line of code.

 print(f'String without whitespaces: {newtext}')

It's supposed to be.

print('String without whitespaces: ', output_without_whitespace(text))

I realize that my problem as to why the sentence without whitespaces was not printing back out to me was, because I was not calling out my function!

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
Vee
  • 15
  • 5

4 Answers4

1

You have the right idea, but here's how to implement it with split and join:

def output_without_whitespace(text):
  return ''.join(text.split())

so that:

output_without_whitespace(' this\t  is a\n test..\n ')

would return:

thisisatest..
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • I enter in this code, however, I'm still getting the same string WITH white spaces. – Vee Sep 28 '18 at 05:09
  • Did you copy and paste this code? There must be a typo when you entered the code. Please make sure you are not passing any argument to `split()`. – blhsing Sep 28 '18 at 05:11
  • Yes, I did. Now, nothing is being printed out. – Vee Sep 28 '18 at 05:12
  • Oh, i figured out my problem – Vee Sep 28 '18 at 05:13
  • If you want it to print, you should just `print` it with `print(output_without_whitespace(' this\t is a\n test..\n '))`. – blhsing Sep 28 '18 at 05:13
  • Yes, I just realized my problem. I edited my question with the right code, fixing my problem haha – Vee Sep 28 '18 at 05:18
1

A trivial solution is to just use split and rejoin (similar to what you are doing):

def output_without_whitespace(text):
    return ''.join(text.split())

First we split the initial string to a list of words, then we join them all together.

So to think about it a bit:

text.split()

will give us a list of words (split by any whitespace). So for example:

'hello world'.split() -> ['hello', 'world']

And finally

''.join(<result of text.split()>)

joins all of the words in the given list to a single string. So:

''.join(['hello', 'world']) -> 'helloworld'

See Remove all whitespace in a string in Python for more ways to do it.

csm10495
  • 569
  • 6
  • 12
0

There are a few different ways to do this, but this seems the most obvious one to me. It is simple and efficient.

>>> with_spaces = '  The  quick brown       fox  '
>>> list_no_spaces = with_spaces.split() 
>>> ''.join(list_no_spaces)
'Thequickbrownfox'

.split() with no parameter splits a string into a list wherever there's one or more white space characters, leaving out the white space...more details here.

''.join(list_no_spaces) joins elements of the list into a string with nothing betwen the elements, which is what you want here: 'Thequickbrownfox'.

If you had used ','.join(list_no_spaces) you'd get 'The,quick,brown,fox'.

Experienced Python programmers tend to use regular expressions sparingly. Often it's better to use tools like .split() and .join() to do the work, and keep regular expressions for where there is no alternative.

Nic
  • 1,518
  • 12
  • 26
  • Did you even test this? – CristiFati Sep 28 '18 at 04:37
  • My bad-- writing to much Groovy these days. Fixed. – Nic Sep 28 '18 at 04:59
  • While this might answer the authors question, it lacks some explaining words and links to documentation. Raw code snippets are not very helpful without some phrases around it. You may also find [how to write a good answer](https://stackoverflow.com/help/how-to-answer) very helpful. Please edit your answer. – hellow Sep 28 '18 at 07:11
  • Thanks for the feedback. Explanation added. I wrote the original answer in a hurry on a phone, which is not a good look. I did actually write an explanation but somehow lost it on the StackOverflow app and had no time to rewrite. – Nic Sep 30 '18 at 23:42
0

Get input, split, join

s = ''.join((input('Enter string: ').split()))
Enter string: vash the stampede
vashthestampede
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20