简体   繁体   English

给定一个 string,将其偶数索引和奇数索引字符作为空格分隔的字符串打印在一行上

[英]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 => Print abc 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.

相关问题 给定一个字符串 S,将其偶数索引字符和奇数索引字符作为 2 个空格分隔的字符串打印在一行中 - Given a string, S , print its even-indexed and odd-indexed characters as 2 space-separated strings on a single line 如何打印字符串的偶数索引和奇数索引字符? - How to print the even-indexed and odd-indexed characters of strings? 对于每个 String S,打印 的偶数索引字符,后跟一个空格,然后是 的奇数索引字符 - For each String S, print 's even-indexed characters, followed by a space, followed by 's odd-indexed characters Pythonic方法将字符串列表转换为字典,奇数索引字符串作为键,偶数索引字符串作为值? - Pythonic way to turn a list of strings into a dictionary with the odd-indexed strings as keys and even-indexed ones as values? 替换字符串中的奇数和偶数索引字符 - Replacing Odd and Even-indexed characters in a string 将数组的偶数索引元素乘以 2,将数组的奇数索引元素乘以 3 - Multiply even-indexed elements of array by 2 and odd-indexed elements of array by 3 尝试从字符串中打印偶数索引字符 - Trying to print the even indexed characters from a string 将字符串列表转换为以空格分隔的字符串 - Convert list of strings to space-separated string 从 Python 中的列表中删除奇数索引元素 - Remove odd-indexed elements from list in Python 如何在列表中找到奇数索引值的乘积 - How to find the product of the odd-indexed values in a list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM