Reputation: 25
I've made a program as follows:
x = input('Message? ')
x = x[::3]
for i in x:
print(i, end=' ')
it's supposed to give me the letters of input (every third letter) which it does, although it also prints a white space at the end which I have been unable to get rid of. I've tried everything including the .rstrip
and [:-1]
with no luck
Upvotes: 0
Views: 83
Reputation: 113915
Try this:
x = input('Message? ')
x = x[::3]
print(' '.join(x[::3]))
join
lets you put a space between adjacent characters, leaving out the trailing white-space
Upvotes: 0
Reputation: 1121524
You are telling print()
to print that whitespace with end=' '
.
Instead of calling print()
repeatedly, pass in the whole list in one go:
print(*x)
Now each element of x
is printed as a separate argument, using the default 1-space separator, and a newline as end
.
Outside of passing in the elements as separate arguments to print()
, you can also use str.join()
to build one string with separators; this does require that all elements in x
are strings or you'd need to explicitly convert them:
print(' '.join(x))
Upvotes: 2