why Storing multiple variables is creating a tuple
why Storing multiple variables is creating a tuple
Hi i am writing a code but unfortunately when i try to store multiple variables from a list or tuple into a new variable it just makes a new tuple
content=['2.0', 'Banana']
newli = content[0],'t',content[1]
print(newli)
It should be printing like a string but it is printing as a tuple in round brackets. I need this tab spacing in between these two variables. Any help would be really appreciated.
by doing content[0]+'t'+content[1]
the tab space is not working
content[0]+'t'+content[1]
Actually what i wanted to achieve it so i can call this variable anywhere without using the manual print command everywhere and also to write in a file
Do consider using
join
instead as the last comment says.– doctorlove
Jun 29 at 8:41
join
7 Answers
7
Replace the commas with + i.e.
newli = content[0] + 't' + content[1]
by doing this the tab space is not coming
– Noob Saibot
Jun 29 at 8:42
When I run it the tab is there.
– jc1850
Jun 29 at 8:45
Its strange when i check on repl.it space it coming but not in my pycharm
– Noob Saibot
Jun 29 at 8:47
Your pycharm console might be rendering the tab as 0 spaces, you could replace
't'
with 4*' '
to make sure it works on all consoles.– jc1850
Jun 29 at 8:54
't'
4*' '
newli
is a tuple, since you're grouping values with a comma.
newli
You should make it a string by doing this:
content=['2.0', 'Banana']
newli = content[0] + 't' + content[1]
print(newli)
This is normal behavior:
a = 'first', 't', 'second'
creates a tuple containing 'first'
, and tab and 'second'
a = 'first',
creates a tuple containing 'first'
; it is the comma that makes the tuple.
a = 'first', 't', 'second'
'first'
'second'
a = 'first',
'first'
If you want to concatenate the values as a string, you could do like this:
a = 'first' + 't' + 'second'
a = 'first' + 't' + 'second'
or like this, using f-strings
:
f-strings
f = 'first'
m = 't'
s = 'second'
a = f'{f}{m}{s}'
You are not using string concatenation, but creating a tuple. You use ,
separation to create a tuple. You need to use +
to concatenate strings.
,
+
content=['2.0', 'Banana']
newli = content[0] + 't' + content[1]
print(newli)
by doing this the tab space is not coming
– Noob Saibot
Jun 29 at 8:42
Its because you are asking for newli to store items as a tuple.
Use string concatenation if you want to store them as a string
Yeah that's because you are putting comas between the strings. In python string concatenation is done with the +
operator.
+
Try changing your code to:
content=['2.0', 'Banana']
newli = content[0] + 't' + content[1]
print(newli)
by doing this the tab space is not coming
– Noob Saibot
Jun 29 at 8:41
In Python values that are separated by comma (,
) produces a tuple of that values.
Like:
,
a=10
b=5
total_data = a,b
print(total_data) #this will produce a tuple (10, 5)
To get output as string
you need to use +
operator:
string
+
content=['2.0', 'Banana']
newli = content[0]+'t'+content[1]
print(newli) #output: 2.0 Banana
Also you can use join
function to get your expected output. Like:
join
content=['2.0', 'Banana']
newli = content[0],content[1]
print("t".join(newli)) #output: 2.0 Banana
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Possible duplicate of Concatenate item in list to strings
– Georgy
Jun 29 at 8:38