python - 对于范围内的我(len(名字)): print(firstname[0]) - How do i print this in one line?

标签 python python-3.x

我目前正在学习 Python 编码,我迫不及待地想掌握它。我现在有两周时间,正在做一些练习。所以这就是为什么这个问题(以及我将来可能会问的其他问题:D)对您来说可能微不足道。

因此,这里的目标是询问用户的名字和姓氏。之后,按保留顺序打印名字中的字符 (Jack = 4*J) 和姓氏 (Dullname = emanlluD) 中的第一个字母。

好吧,我已经确定了该部分,但现在我找不到解决方案,如何将所有内容打印在一行中。因为名字来自列表,打印内容是:

Give your first name: Jack
Give your last name: Dullname
J
J
J
J
emanlluD

结果应为 Jack Dullname = JJJJ emanlluD。

firstname =  str(input("Give your first name: "))
lastname = str(input("Give your last name: "))

for i in range(len(firstname)):
    print(firstname[0])

def reverse(x):
    return x[::-1]

output = reverse(lastname)

print(output)

这里的问题是For循环部分。我现在应该是函数,结果应该与第二个输出一起打印,但我只是不知道如何对该结果进行排序。

感谢您的帮助!

编辑:哇!太快了!我真的很感谢所有的答案!我的训练代码现在可以运行了!

最佳答案

乘法即可。

print(firstname[0] * len(firstname), lastname[::-1])

关于python - 对于范围内的我(len(名字)): print(firstname[0]) - How do i print this in one line?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69268183/

相关文章:

python - 如何从数据框中的字符串中提取年份和月份

python - 当我在 Windows Vista 命令提示符下运行 'import pylons' 时,为什么无法识别 Pylons?

python - 导入 GDAL 会打印很多错误消息,但仍然有效

python - Aptana Studio (Windows) 删除了使用 py_compile.compile 编译的 PYC 文件

python - 如何在 x 值的大小而不是计数之后排列 pandas barplot 中的条形

python - 如何从一行覆盖多行(iloc/loc 差异)?

python - 在 python 中查找对称对

python - 转换新数据集以在 Python 中进行预测

mysql - Flask 与 mysqldb 集成

python - 为什么我的直方图条都显示频率为 1