python - Remove whitespace from ONLY last item in list -


how remove last whitespace code? when omit end=" " in print, removes whitespace , don't want that.

r = input("line: ") while true:     n = r.split()     if r == "":       break       else:       word in n:         print(word[::-1], end=" ")       r = input("\nline: ") 

without end=" ", when input "hello world", outputs "ollehdlrow". when add end=" " instead, prints whitespace this: "olleh dlrow ".

how can obtain this: "olleh dlrow"?

the correct way of interposing character between list values str.join:

print(' '.join(word[::-1] word in n)) 

example

>>> n = ['hello', 'world'] >>> ' '.join(word[::-1] word in n) 'olleh dlrow' 

code improvements

your code written this:

while true:     line = input("line: ")     if not line:         break     print(' '.join(word[::-1] word in line.split())) 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -