Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I'm composing a program that checks whether the string is a single word. For what reason doesn't this work and is there any better method to check if a string has no spaces/is a solitary word. 

' ' in word == True

1 Answer

0 votes
by (26.4k points)

The == outweighs in, so you're really trying word == True. 

>>> w = 'ab c'

>>> ' ' in w == True

1: False

>>> (' ' in w) == True

2: True

However, you don't require == True by any means. in the event that requires [something that evalutes to True or False] and ' ' in word will evalute to True or False. Thus, if ' ' in word: ... is okay:

>>> ' ' in w

3: True

Wanna become a Python expert? Come and join the python certification course and get certified.

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...