我在输入中有一些字符串值,类似的东西:
hellOWOrLD.hELLOWORLD.
在输出中我需要:
Helloworld. Helloworld.
或输入:
A.B.A.C.A.B.A.
和输出:
A. B. A. C. A. B. A.
,如您所见,我需要DOT分隔的单词。任务的规则是,如果无法修改输出,则输出为1。
所以我试图这样做:
import sys
input = raw_input().lower().split('.')
for el in input:
sys.stdout.write(el.capitalize() + '.',)
所以这不是很好的代码。你能帮我吗?
您可以使用re.sub
和IF语句检查以下方式进行检查:
import re
usrinput = raw_input()
pretty = " ".join([x.capitalize() for x in re.sub('.','. ', usrinput.lower()).split()]).strip()
if pretty == usrinput:
print 1
else:
print pretty
输入:
hellOWOrLD.hELLOWORLD.
输出:
Helloworld. Helloworld.
input2:
A.B.A.C.A.B.A.
输出2:
A. B. A. C. A. B. A.
input3:
Helloworld. Helloworld.
输出3:
1
这是一种似乎有效的方法:
input = # get input from somewhere
output = '. '.join([ piece.capitalize() for piece in input.split('.') ])
例如。如果input
是"hellOWOrLD.hELLOWORLD."
,则output
为"Helloworld. Helloworld. "
。
如果您想摆脱最终空间,请使用:
output = output.strip(' ')
如果您希望output
是1
,则在没有进行更改的情况下,请执行此操作:
if input == output:
output = 1