以下是一个示例代码,可以按列获取每个字符串的每个字符:
strings = ["apple", "banana", "cherry"]
max_len = max(len(s) for s in strings) # 获取字符串列表中最长的字符串长度
for i in range(max_len):
for s in strings:
if i < len(s):
print(s[i])
else:
print(" ")
输出结果为:
a b c
p a h
p n e
l a r
e n r
a y
n
a
以上代码首先找到字符串列表中最长的字符串的长度,然后通过两个嵌套的循环,按列遍历每个字符串的字符。如果当前列小于字符串的长度,则打印该列的字符,否则打印一个空格。