2

I am trying to not allow any strings whatsoever in the inputted string. I've tried using len strip to try and count the whitespaces and no allowing it but it seems to only count the initial whitespaces and not any in between the inputted string. the objective of this code is: input will not allow spaces.

while True:

  try:
    no_spaces = input('Enter a something with no spaced:\n')
    if len(no_spaces.strip()) == 0:
        print("Try again")
        
    else:
        print(no_spaces)
        
 
except:
    print('')
6
  • str.split will split on whitespace - maybe you could make use of that. did you check the documentation? - docs.python.org/3/library/stdtypes.html#string-methods
    – wwii
    Jul 17, 2021 at 3:24
  • 1
    yeah i tried split and couldn't get it to work. Seems that strip could have better implications so I went with that Jul 17, 2021 at 3:25
  • how about str.count?
    – wwii
    Jul 17, 2021 at 3:26
  • If your goal is to remove white spaces then check this out
    – Abhi
    Jul 17, 2021 at 3:28
  • im pretty sure I have a redundancy in the while True loop that is causing it to not execute correctly. it count initial whit spaces but not spaces in between inputted words. so I dont know if changing that str.() will change anything Jul 17, 2021 at 3:28

2 Answers 2

1

This code will only accept inputs that don't have any spaces.

no_spaces = input('Enter a something with no spaces:\n')
if no_spaces.count(' ') > 0:
    print("Try again")
else:
    print("There were no spaces")

double alternatively

while True:
    no_spaces = input('Enter a something with no spaces:\n')
    if no_spaces.find(' ') != -1:
        print("Try again")
    else:
        print("There were no spaces")
        break

alternatively

while True:
    no_spaces = input('Enter a something with no spaces:\n')
    if ' ' in no_spaces: 
        print("Try again")
    else:
        print("There were no spaces")
        break
3
  • This solved it. Thank you. kinda sucks that .find was the right statement. I wonder if I could have pulled it off with .strip or .split Jul 17, 2021 at 3:38
  • 1
    I believe find is one of the better ways to go about this. It makes very little sense to use strip here. But it is possible to use split like so: len(no_spaces.split(' ')) != 1:
    – 0x263A
    Jul 17, 2021 at 3:40
  • Or even simpler: if ' ' in no_spaces: (that's quote-space-quote)
    – sj95126
    Jul 17, 2021 at 3:44
0

So, if I understand you correctly you don't want ANY spaces in the string? strip() just removes the spaces at the start and end, if you want to remove all spaces in a string you would use something like the replace method. Replace will remove all occurrences of a character and replace them with another (an empty string in this case).

Example:

def main():
    myString = "Hello World!"
    noSpaces = myString.replace(" ", "")
    print(noSpaces)

main()

This code will output "HelloWorld!"

1
  • I want to count the white spaces in the inputted text and if the white space != 0 then it gives them an error and lets them try again and input another string. thats why I have the if len(no_spaces.strip()) == 0: statement because I want to be able to count the spaces and my code execute based on the # Jul 17, 2021 at 3:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.