我想写一个剧本需要一个字和印刷前三个字符,最后3个字符,不管位于正中间点:参数扩展使用Python
abracabra
ABR .. .bra
我做了工作,
word = input("What's the word ?")
first = str(word[0:3])
last = str(word[-3:])
middle = int(len(word)-6)
midDOTS = "." * (middle)
print((first)+(midDOTS)+(last))
,但我想这样做在同一行,就像我可以在bash做,例如它返回的网络接口的列表:
INTFACES=$(/sbin/ifconfig -a | sed 's/[ \t].*//;/^\(lo\|\)$/d')
如何用python做到这一点?我试过了,但没有奏效:
word = input("What's the word ?")
midDOTS = "." * (int(len(word)-6))
print(str(word[0:3])+(midDOTS)+ str(word[-3:]))
什么是正确的语法?
编辑
感谢大家对我的帮助,不仅得到这个正确的,但把它理解为好。以下是我结束了去...
def print_dotted_word():
word = str(input("What's the word ?"))
if len(word)<7:
raise ValueError("Needs to be at least 7 letters.")
print(word[:3] + '.'*(len(word)-6) + word[-3:])
while True:
try:
print_dotted_word()
break
except ValueError:("Needs to be at least 7 letters.")
你有一个语法错误,'midDOTS = “” *(int(len(word)''应该是'midDOTS =“。”*(int(len(word)-6))' – dstudeba
是的,我刚修好了,谢谢,但脚本仍然不能运行。 –
Oh waiting - You are right!It does now now!非常感谢。 –