如何在不同的行上打印每个字母两次?

  • 本文关键字:两次 打印 python
  • 更新时间 :
  • 英文 :

s = input('Enter some text: ')
doubled_s = '';
for c in s:
doubled_s = doubled_s + c*2 + n

输入:

testing

预期输出:

tt
ee
ss
tt
ii
nn
gg

我希望每个字符加倍,下一个字符加倍,但在下一行打印。

你的代码几乎是正确的:

s = input('Enter some text: ')
doubled_s = '';
for c in s:
doubled_s = doubled_s + c*2 + 'n'  # missing quotes
print(doubled_s)                        # print final result

为什么不在循环中直接打印呢?

s = input('Enter some text: ')
for c in s: 
print(c+c)

我尊重@mozway的答案,这里它以一种更python的方式。

s = input('Enter some text: ')
doubled_s = ''
for c in s:
doubled_s += c*2 + 'n'
print(doubled_s)

尝试直接在循环内打印,对于下一个元素,它将直接在下一行打印,不需要输入"n"。代码:

s=input("Enter: ")
for c in s:
print(2*c)

try:

doubles_s += c * 2 + 'n'

通过这样做,它将把下一行语法视为字符串并添加到double_s中,而不是将其视为对象

最新更新