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

问题描述 投票:-2回答:2

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

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

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

(如果可以,请尝试解释您的答案)样品输入输出:输入:

  • H
  • E
  • L
  • L
  • O

输出:HELLO ABC

python string short
2个回答
0
投票

将水平转换为垂直:您只需要在字符串中的每个字符之后输入换行符(\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'

0
投票
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%为我的程序工作

© www.soinside.com 2019 - 2024. All rights reserved.