简体   繁体   English

在 python 中乘 n 次后每个字符之间添加空格

[英]Add space between each character after it is multiplied n times in python

So basically we have to enter 2 lists.所以基本上我们必须输入 2 个列表。 Then each of the list elements is added to its corresponding element (as shown in the example below) to create a sum list.然后将每个列表元素添加到其对应的元素(如下例所示)以创建总和列表。 Then we take a string and take the first character of the string.然后我们取一个字符串,取字符串的第一个字符。 We then multiply the character with the numbers from the sum list.然后我们将字符与总和列表中的数字相乘。 The problem I'm having is that I want to add a space every time the character is repeated (as shown below).我遇到的问题是我想在每次重复字符时添加一个空格(如下所示)。

This is my code:这是我的代码:

l1 = [i for i in eval(input("Enter a list: "))]
l2 = [i for i in eval(input("Enter another list: "))]
symbol = input("Enter symbol: ")


summation = []
for (i1, i2) in zip(l1, l2):
    summation.append(i1+i2)

i = 0
while i < len(summation):
    print(summation[i] , symbol[0]*summation[i])
    i += 1

This is how my output looks:这就是我的 output 的外观:

Enter a list: 1,2,3
Enter another list: 1,2,3
Enter symbol: hello world
2 hh
4 hhhh
6 hhhhhh

This is how I want my output to look:这就是我希望我的 output 看起来的样子:

Enter a list: 1,2,3
Enter another list: 1,2,3
Enter symbol: hello world
2 h h
4 h h h h
6 h h h h h h

Can someone please help me?有人可以帮帮我吗? It might be a stupid question but I would really appreciate it!这可能是一个愚蠢的问题,但我真的很感激!

Replace代替

symbol[0]*summation[i]

with

(symbol[0] + " ")*summation[i]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python 中每个字符之间的空格 - Space between each character in python Python pandas 加 N 乘以当前秒的值,再加上 N 乘以前一秒的值 - Python pandas add N multiplied by the value of the current second and add with the value of N multiplied by the value of the previous second 如何从python函数返回字符串n次,以便打印n次,中间有空格 - how to return string n times from a python function, such that it is printed n times with space in between 如何在相乘的字符串之间添加空格? - How can I add space between the strings which are multiplied? 在 python 中的字符串字符之间添加空格 在 python 中使用“for”循环 - add space between character of string in python use “for” loops in python 使用 python 在输入中插入的每个字符后打印一个带空格的单词 - Print a word with space after each character that was inserted in an input using python python正则表达式在数字和给定字符之间添加空格 - python regex add space between digits and a given character 提取 python dataframe 中关键字后空格之间的所有字符 - Extract all the character between space after a keyword in python dataframe 如何在Python中使用正则表达式在特定字符之前和之后添加空格? - How to add space before and after specific character using regex in Python? 如何通过在 Python 中添加 N 数乘以 M 倍等获得所需的 output - How to get the desired output by adding N number multiplied by M times and so on in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM