Python 的参数扩展

Parameter Expansion With Python

我正在尝试编写一个脚本,输入一个单词并打印前三个字符、后三个字符以及位于中间的任何内容:

咒语

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 中所做的那样,例如这个 returns 网络接口列表:

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.")

您可以执行以下操作:

word = input("What's the word ?")
if len(word)<7:
    raise ValueError("Please enter a word greater than 6 characters")
print(word[:3] + '.'*(len(word)-6) + word[-3:])

在这里,如果输入的 word 少于 7 个字符,我们将引发 ValueError 异常。

我们可以在 Python shell 中通过将此代码包含在函数 print_dotted_word().

中进行检查

Python 2.7:

In [1]: def print_dotted_word():
            word = raw_input("What's the word ? \n") # use raw_input
            if len(word)<7: # check for word length
                raise ValueError("Please enter a word greater than 6 characters") # raise exception
            print word[:3] + '.'*(len(word)-6) + word[-3:] # print desired response

In [2]: print_dotted_word()
What's the word ? 
helloworld
hel....rld

In [3]: print_dotted_word()
What's the word ? 
hello
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
----> 1 print_dotted_word()
      2     word = raw_input("What's the word ? \n")
      3     if len(word)<7:
----> 4         raise ValueError("Please enter a word greater than 6 characters")
      5     print word[:3] + '.'*(len(word)-6) + word[-3:]

ValueError: Please enter a word greater than 6 characters

Python 3.4:

In [1]: def print_dotted_word():
            word = input("What's the word ? \n") # use input here
            if len(word)<7: # check for word length
                raise ValueError("Please enter a word greater than 6 characters") # raise exception 
            print(word[:3] + '.'*(len(word)-6) + word[-3:]) # print desired response

In [2]: print_dotted_word()
What's the word ? 
helloworld
hel....rld

In [3]: print_dotted_word()
What's the word ? 
hello
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
----> 1 print_dotted_word()
      2     word = input("What's the word ? \n")
      3     if len(word)<7:
----> 4         raise ValueError("Please enter a word greater than 6 characters")
      5     print(word[:3] + '.'*(len(word)-6) + word[-3:])
      6 
ValueError: Please enter a word greater than 6 characters