[英]Given a string, print its even-indexed and odd-indexed characters as space-separated strings on a single line
Given a string, print its even-indexed and odd-indexed characters as space-separated strings on a single line.给定一个 string,将其偶数索引和奇数索引字符作为空格分隔的字符串打印在一行上。
Example:例子:
s = adbecf
=> Printabc def
s = adbecf
=> 打印abc def
My approach:我的做法:
t = input() p = len(t) for i in range(p): s = t[i] n = len(s) even = [] odd = [] for j in range(n): if j % 2 == 0: even.append(s[j]) for j in range(n): if j % 2:= 0. odd.append(s[j]) first_join=''.join(even) second_join = ''.join(odd) print("{} {}",format(first_join second_join)
No need to use complex ways.无需使用复杂的方法。 here is a easy way to do it.这是一个简单的方法。
t = input() p = len(t) ans = "" # create empty string for i in range(0,p,2): ans += t[i] # Add even characters ans += " " # Add space. for j in range(1,p,2): ans += t[j] # Add odd characters print(ans)
Input: adbecf
输入: adbecf
Output: abc def
Output: abc def
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.