简体   繁体   English

如何使用 python 将字符串中的元素相乘并将结果字符串存储在变量中?

[英]How can I multiply elements in a string using python and store resulting string in a variable?

Here is the Problem needed to be solved:这是需要解决的问题:

This problem requires you to create a output string from input string such that for every character in input string, there are three same characters in output string.('Hello' is the input sting) ("HHHeeellllllooo" is the desired output.) This problem requires you to create a output string from input string such that for every character in input string, there are three same characters in output string.('Hello' is the input sting) ("HHHeeellllllooo" is the desired output.)

This is what I have tried:这是我尝试过的:

input_string = "Hello"
output_string = ""
N = 3

for i in input_string:
    strings = i * 3
    print(strings, end = "")

How can I store the 'expanded' string to output_string?如何将“扩展”字符串存储到 output_string?

input_string = "Hello"
output_string = ""
N = 3

for i in input_string:
    strings = i * 3
    output_string += strings

print(output_string)

As far as I knew, inside the loop you only need to assign the value of by output_string += i * 3 instead of strings = i * 3 .据我所知,在循环内,您只需要分配 by output_string += i * 3而不是strings = i * 3的值。 In this case you are keeping the pervious value, beside that you are also adding the new value to the pervious.在这种情况下,您将保留 pervious 值,此外,您还将新值添加到 pervious 中。 the new code will be like:新代码将如下所示:

input_string = "Hello"
output_string = ""
N = 3

for i in input_string:
    output_string += i * 3
print(output_string, end = "")

Instead of printing it, add it to output_string .不要打印它,而是将它添加到output_string

input_string = "Hello"
output_string = ""
N = 3

for i in input_string:
    output_string += i * 3 # means same thing as output_string = output_string + i*3

print(output_string) # Output: HHHeeellllllooo

The key point here, is that you can use + to concatenate strings:这里的关键点是您可以使用+来连接字符串:

a = "Hello, "
b = "World!"

a + b # Output: Hello, World!

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

相关问题 如何在Python中调用带有存储为字符串的变量的函数 - How in Python can I call a function with a variable which is store as a string 如何在python中将字符串乘以变量整数 - How to multiply a string by a variable integer in python 将 Python 程序转换为 C:如何将字符乘以指定值并将其存储到变量中? - Converting Python program to C: How can I multiply a character by a specified value and store it into a variable? 如何在python中使用字符串获取变量 - How can i get a variable using a string in python 如何将存储在变量中的函数结果存储为字符串? - How can I store the result of a function stored in variable as a string? 如何在python中将html文件作为字符串存储在变量中 - How to store a html file in a variable as a string in python 如何使用python使用嵌套for循环反转列表及其字符串元素? - how can i reverse a list as well as its string elements using python using nested for loops? 在Python中,为什么我可以将一个字符串乘以一个数字,但我不能将一个字符串和一个数字相加? - In Python, why can I multiply a string by a number, but I can't add a string and a number? python - 将字符串的元素存储到列表中 - python - Store the elements of a string into a list 如何使用python将带有混合引号的字符串存储在Sqlite数据库中? - How can I store a string with mixed quotes in a Sqlite database using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM