Python的参数扩展

时间:2015-09-26 03:37:15

标签: python bash expansion

我正在尝试编写一个脚本接受一个单词并打印前三个字符,最后三个字符,以及点中间的任何内容:

abracabra

... ABR胸罩

我做到了,

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

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

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:])

如果输入的ValueError少于7个字符,我们会在此处引发word例外。

我们可以通过将此代码包含在函数print_dotted_word()中来检查Python shell中的内容。

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