I am trying to write a script takes a word and prints the first three characters, the last 3 characters, and whatever lies in the middle in dots:
abracabra
abr...bra
I made it work,
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))
but I would like to do it on one line, like I can do in bash, for example this returns a list of network interfaces:
INTFACES=$(/sbin/ifconfig -a | sed 's/[ \t].*//;/^\(lo\|\)$/d')
How do I do this with python? I tried this, but it did not work:
word = input("What's the word ?")
midDOTS = "." * (int(len(word)-6))
print(str(word[0:3])+(midDOTS)+ str(word[-3:]))
What is the correct syntax?
Edit
Thanks to everyone for helping me not only get this correct, but understand it as well. Here is what I ended up going with...
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.")
You can do the following:
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:])
Here, we will raise a ValueError
exception if the word
entered is less than 7 characters.
We can check this in Python shell by enclosing this code in a function 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