Strike
Strike

Reputation: 95

Python - String Stored in a Variable showing as tuple

I performing concatenation of multiple variables to a string and trying to store it in a list. I want the list to have the string but when i append the list in a loop, the value on the list is shown as tuple. Please help, New to python :)

When i Print :

print ( key +' ' +' =', amount1+' ' +' /',amount2+' ' +' /',pincrease)

Output is in str :

app1  = 53.58  / 54.81  / 2.24% lower.

When i append my list :

message = ( key +' ' +' =', amount1+' ' +' /',amount2+' ' +' /',pincrease)
message.append(mylist)
print(mylist)

output :

[('app1  =', '53.58  /', '54.81  /', '2.24% lower.')]

I would like to get the value as string in the list.. as

["app1  = 53.58  / 54.81  / 2.24% lower."]

Upvotes: 1

Views: 1573

Answers (2)

Kushan Gunasekera
Kushan Gunasekera

Reputation: 8556

You code is actually working fine, but there is a some small mistake when you append your message into the list.

mylist.append(''.join(message))  # this step will remove your tuple
print(mylist)

# ["app1  = 53.58  / 54.81  / 2.24% lower."]

Upvotes: 2

Joshua Smith
Joshua Smith

Reputation: 6621

First, this code:

( key +' ' +' =', amount1+' ' +' /',amount2+' ' +' /',pincrease)

is a tuple. You might consider using format strings instead of + for this kind of thing. Which would make it look more like this:

message = f'{key} = {amount1} / {amount2} / {pincrease}'
mylist.append(message)

Upvotes: 1

Related Questions