[英]How to do slicing when using sys.stdout.write()
I have an input like this: 我有这样的输入:
country = 'CANADA'
and I want the output to be like this: 我希望输出是这样的:
C*A*N*A*D*A
I tried to do this: 我试图这样做:
for letter in country:
tag = letter+'*'
sys.stdout.write(tag)
But I will get C*A*N*A*D*A*
instead of C*A*N*A*D*A
. 但是我会得到
C*A*N*A*D*A*
而不是C*A*N*A*D*A
So how do I slice the sys.stdout.write
or do I have to do other format? 那么,如何切片
sys.stdout.write
或必须执行其他格式?
>>> country = 'CANADA'
>>> '*'.join(country)
'C*A*N*A*D*A'
Your version with slicing: 带有切片的版本:
>>> country = 'CANADA'
>>> tag = ''
>>> for letter in country:
tag += letter+'*'
>>> sys.stdout.write(tag[:-1]) # everything up until the last character
C*A*N*A*D*A
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.