-1

I wrote the following code :

lst=["Terribly Tricky"] 
for word in lst:
    for letter in word[-6:]:
        print(letter)

And I was excpecting python to print "Tricky" but it returned the following :

T
r
i
c
k
y

How can I get it to print it like this :

Tricky

Thanks in advance.

2 Answers2

2

Others have suggested viable solutions. But, if you want to go with nested for loops, here is something you can try.

    lst=["Terribly Tricky"] 
    for word in lst:
        s = ""
        for letter in word[-6:]:
            s += letter
        print s

If you want to print the word but are okay with spaces between letters, just put a comma after the print. It will print on the same line and add a space between prints - useful when printing list items.

    for word in lst:
        for letter in word[-6:]:
            print (letter),

This prints T r i c k y

Note: please re-indent the code block if using the python code.

mhaseebmlk
  • 212
  • 2
  • 5
1

So, what you can do is just split the string inside lst. In your case, splitting will create a list of strings. And then you can print the string that you want.

lst=["Terribly Tricky"]

for word in lst:
    words = word.split(' ')
print (words[1])

This will print "Tricky" in one line.

Vico
  • 579
  • 3
  • 13