我尝试使用这些脚本,但是它们不起作用。 (x是垂直变量)
print "".join(x.split())
和
x.replace("\n", "")
(如果可以,请尝试解释您的答案)样品输入输出:输入:
输出:HELLO ABC
将水平转换为垂直:您只需要在字符串中的每个字符之后输入换行符(\n
)。
s1 = "abcd1234" # some horizontal string
s2 = "" # another string: we will use this to store verical form of s1
for i in s1:
s2 = s2 + i + '\n'
将垂直转换为水平:您只需要删除字符串中每个字符之后的换行符(\n
)。
s1 = "a\nb\nc\nd\n1\n2\n3\n4\n" # some horizontal string
s2 = "" # another string: we will use this to store horizontal form of s1
for i in s1:
if i != '\\n':
s2 = s2 + i + '\n'
input=(a\nb\nc\nd\nA\nB\nC\n) #<---Vertical text
result=input.replace("\n", "") #Convert the vertical text to horizontal text
print(result) #Print the horizontal text
输出:abcABC
100%为我的程序工作