Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJohn Piaz
1,713 PointsHow to join two tuples into a string?
I have two tuples, one being stored in the variable n, and the other stored in x. I want to join then together into one, final variable that is a string. How can I do this? I tried this but it did not work:
final = "".join(n) + "".join(str(x))
I am stringing x because it says it can't add STR and INT together
2 Answers
Chris Freeman
Treehouse Moderator 68,454 PointsThe shortest answer would be
add n to tuple the convert
final = “”.join(x+(str(n),))
# or simple concatenation
final = “”.join(x) + str(n)
# or f-string
final = f”{‘’.join(x)}{n}”
If x and n are both tuples and some values in both tuples are not strings, there are three steps:
- join tuples
- Convert to strings
- Join strings
Final = “”.join([str(item)
for item in x+n])
John Piaz
1,713 PointsIt also says:
TypeError: sequence item 0: expected str instance, int found
Am I doing something wrong?
John Piaz
1,713 PointsJohn Piaz
1,713 PointsWhen I print it out, there are a bunch commas and parentheses:
(2, 3, 7) ('a', 'a', 'a', 'a', 'a', 'a')
Chris Freeman
Treehouse Moderator 68,454 PointsChris Freeman
Treehouse Moderator 68,454 PointsI’ve updated my answer. It might help recommend a specific solution if a sample
x
andn
values are provided.John Piaz
1,713 PointsJohn Piaz
1,713 PointsThank you! I got it to work.