如何在 python 中将垂直字符串更改为水平字符串?

How to change a vertical string to a horizontal string in python?

我试过使用这些脚本,但它们不起作用。 (x为垂直变量)

print "".join(x.split())

x.replace("\n", "")

(如果可以,尽量解释你的答案) 样本输入输出: 输入:

输出: 你好 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% 为我的计划工作

要在 pY3 中将垂直字符串转换为水平字符串,只需将以下内容添加到 op 的打印语句中的结束命令即可。

#when i is your vertical op
#add (",end='')
for i in s1:
    print(i, end='')

它将转换为水平操作。