Addition of tuples

Addition of tuples

Two tuples can be concatenated by using the addition operator.

Let us see the program below:

sites = ("talenteve", "embeddeddesignblog")
sites1 = ("py-programmers", "way2know")
print(sites+sites1)

The output of the above code is 
('talenteve', 'embeddeddesignblog', 'py-programmers', 'way2know')

-----------------------------------

Let us see the below code,

sites = ("talenteve", "embeddeddesignblog")
sites1 = ("py-programmers")
print(sites+sites1)

In the above code, sites1 is treated as string by Python, so, when we try to concatenate, we see the below Type error.

Traceback (most recent call last):
  File "D:\dharanidhar\eclipse-workspace\tuple-python\tuple.py", line 17, in <module>
    print(sites+sites1)
TypeError: can only concatenate tuple (not "str") to tuple

Post a Comment

0 Comments