[英]Adding a new string value to the existing string with comma separated in python
I wanted to add strings to the already existing string with comma separtaed values.我想用逗号分隔值将字符串添加到已经存在的字符串中。
For example old_string = 'a@q' Now if I get one new_string1= 'b@q'例如 old_string = 'a@q' 现在如果我得到一个 new_string1='b@q'
Then what I need is old_string = 'a@q,b@q'那么我需要的是 old_string = 'a@q,b@q'
Then if I get new_string2='c@q'然后如果我得到 new_string2='c@q'
Then What I should get here is old_string='a@q,b@q,c@q'那么我应该在这里得到的是 old_string='a@q,b@q,c@q'
What I tried here is我在这里尝试的是
my_string='a@q'
>>> new_string='b@q'
>>> my_string=','.join(new_string)
>>> my_string
'b,@,q'
But that is not output I am expecting here.但这不是我在这里期望的输出。 I am new to python .Can some one help me in this
我是 python 新手。有人可以帮助我吗
.join
usually adds items in lists. .join
通常在列表中添加项目。 I will concatenate two strings with +
operator and add to my_string:我将使用
+
运算符连接两个字符串并添加到 my_string:
my_string+=","+new_string
Now my_string will be 'a@q,b@q'
现在 my_string 将是
'a@q,b@q'
If you have two different variables and want to concatenate them:如果您有两个不同的变量并希望将它们连接起来:
old_string='a@q'
new_string='b@q'
my_string=old_string+","+new_string
您可以使用可迭代列表并join
其中。
','.join([old_string, new_string])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.