【发布时间】:2021-11-09 04:49:44
【问题描述】:
我需要编写代码来检查某个输入字符串是否有效。它必须:
- 在输入字符串中包含一个用于分隔单词的“空格”
- 一行中不包含多个连续空格
- 不只包含一个“空格”(只 单个空格作为输入)。
这就是我的意思:
username = str(input())
print(username)
username = "two apples" # acceptable
username = "two apples and pears" # acceptable
username = "two' '' 'apples" # not acceptable (because of 2 spaces in a row or more)
username = " " # not acceptable (because of single space with no other words.)
username = "' '' '' '' '' '' '" #not acceptable because of multiple spaces (didn't know how to type it in here better to clarify.
【问题讨论】:
-
if username == " " or " " in username: print("Invalid username")? -
是的,我试过了,但是如果用户名 == " " # 3 or 4 or5 或任何其他数量的空格,这将不起作用。
-
if username.contains(" ") #double space
-
@Swagrim
.contains是什么? -
@YuraHardzeyevskyy 不,显然你没有尝试过。
标签: python python-3.x string