>>> word = list(input()) # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join(word) # join the characters together for the result
'bcefhiklno'
s = '0123456'
print s[::3]
# 036
# first way(understandable) create a new str
s_new = ''
for i in xrange(len(s)):
if i % 3 != 0:
s_new += s[i]
print s_new
# 1245
# second way
s_lst = [c if i % 3 else '' for i, c in enumerate(s)]
print s_lst
# ['', '1', '2', '', '4', '5', '']
s_new = ''.join(s_lst)
print s_new
# 1245
# you can put it in single line
s_new = ''.join([c if i % 3 else '' for i, c in enumerate(s)])
print s_new
# 1245
# third way
s_idx = filter(lambda x: x[0] % 3, enumerate(s))
print s_idx
# [(1, '1'), (2, '2'), (4, '4'), (5, '5')]
print ''.join([x[1] for x in s_idx])
# 1245
输入字符串是不可变的,但将其转换为列表,您可以对其进行编辑:
从不同的字符开始:
^{pr2}$看看这个:
我的答案是:
相关问题 更多 >
编程相关推荐