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('')
Daniel Avila
  • 71
  • 1
  • 9
  • str.split will split on whitespace - maybe you could make use of that. did you check the documentation? - https://docs.python.org/3/library/stdtypes.html#string-methods – wwii Jul 17 '21 at 03: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 – Daniel Avila Jul 17 '21 at 03:25
  • how about str.count? – wwii Jul 17 '21 at 03:26
  • If your goal is to remove white spaces then [check this out](https://stackoverflow.com/a/8270124/6870223) – Abhi Jul 17 '21 at 03: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 – Daniel Avila Jul 17 '21 at 03:28
  • Or `not any(c == ' ' for c in no_spaces)` or `all(c != ' ' for c in no_spaces)` – wwii Jul 17 '21 at 03:29

2 Answers2

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
0x263A
  • 1,807
  • 9
  • 22
  • 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 – Daniel Avila Jul 17 '21 at 03: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 '21 at 03:40
  • Or even simpler: ```if ' ' in no_spaces:``` (that's ```quote-space-quote```) – sj95126 Jul 17 '21 at 03: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!"

  • 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 # – Daniel Avila Jul 17 '21 at 03:31