Evan Schwartzentruber
Evan Schwartzentruber

Reputation: 546

How could I check an input for multiple blanks (" ")?

a = "Am ate rasu"
if " " in a[a.find(" "):]:
    print("Invalid")
else:
    print(a.upper()[0] + a.lower()[1:a.find(" ")])

I want to "scan" the input - in this case it would be the string value of a - to check if there are more than one or more blanks (" "). In this approach, I tried to do so by checking if there were any blanks after the first one, but my issue is that it will continue to detect the blank at a.find(" ") and I'm not sure how to get around that. I have also tried a.find(" ") + 1 but that didn't work either.

from scan_str_module import full_scan as def1
a = input("Full Name: ")
bool1 = def1(a)
if bool1 is False:
    print("Invalid")
else:
    x = a.find(" ")
    name1 = a.upper()[0] + a.lower()[1:(x)]
    name2 = a.upper()[(x)] + a.lower()[(x)]
    print("First: " + name1)
    print("Last: " + name2)
    print(name1 + " " + name2)

Related Issue (Same Project): The issue lies in the first 3 lines of the else statement. Let's say the input is "john smith". The def1 function will "scan" the input for any numeral values returning as True/False. If it's a valid input, name1 will be declared as every string value from j to n in john and name2 as every string value from s to h in smith. It should then print:

First: John
Last: Smith
John Smith

But it actually prints something like this:

First: John
Last:
John

This happens because the a.find(" ") value is actually specified on the blank (" ") between John and Smith. My issue is that I have no idea how to make it so that name2 can be specified in the correct a.find() value.

Upvotes: 0

Views: 118

Answers (4)

Evan Schwartzentruber
Evan Schwartzentruber

Reputation: 546

Everything functions correctly now:

First issue: (Just used count function)

a = "Am ate rasu"
if a.count(" ") > 1:
    print("Invalid")
else:
    print(a.upper()[0] + a.lower()[1:a.find(" ")])

Second issue: (Just added more value to the [a.find(" "):] values. +1 and +2 to position them right.

name1 = a.upper()[0] + a.lower()[1:a.find(" ")]
name2 = a.upper()[a.find(" ")+1] + a.lower()[a.find(" ")+2:]

Upvotes: 0

Dhaval Taunk
Dhaval Taunk

Reputation: 1672

you can try this also:-

a = "Am ate rasu"
if a.count(' ') > 1:
    #####your code
else:
    #####your code

Upvotes: 2

Prune
Prune

Reputation: 77837

You can use your current code with a tiny modification: take the string slice starting after the first blank, not at the first blank:

if " " in a[a.find(" ")+1:]:

Even easier,

if a.count(' ') > 1:

Upvotes: 3

BadPiggie
BadPiggie

Reputation: 6359

Consider, Following code.

if len(a.split(' ')) > 2:
  # 1 space splits 2 words, So If the length of word list > 2, More than 1 spaces are there...
else:

Upvotes: 2

Related Questions