Eden Crow
Eden Crow

Reputation: 16194

Python 3: Converting A Tuple To A String

I have the following code:

var_one = var_two[var_three-1]
var_one = "string_one" + var_1

And I need to do the following to it:

var_four = 'string_two', var_one

However, this returns the following error:

TypeError: Can't convert 'tuple' object to str implicity

I have tried things such as str(var_one) and using strip but these did not work. What can I do to achieve the result I require?

EDIT - Here are what the variables contain:

var_one: new variable

var_two: tuple

var_three: integer

var_four: new

EDIT2:

The line in the program that makes the error is: os.system(var_four)

Upvotes: 3

Views: 22835

Answers (5)

Scott Skiles
Scott Skiles

Reputation: 3847

str(my_tuple)

This seems too easy, but this works in Python 3.6

>>> x = list(range(100))
>>> y = list(range(500, 600))
>>> zip_obj = zip(x, y)
>>> my_tuple = tuple(zip_obj)
>>> type(my_tuple)
>>> <class 'tuple'>
>>> tuple_str = str(my_tuple)
>>> tuple_str
'((0, 500), (1, 501), (2, 502), (3, 503), (4, 504), (5, 505), (6, 506), (7, 507), (8, 508), (9, 509), (10, 510), (11, 511), (12, 512), (13, 513), (14, 514), (15, 515), (16, 516), (17, 517), (18, 518), (19, 519), (20, 520), (21, 521), (22, 522), (23, 523), (24, 524), (25, 525), (26, 526), (27, 527), (28, 528), (29, 529), (30, 530), (31, 531), (32, 532), (33, 533), (34, 534), (35, 535), (36, 536), (37, 537), (38, 538), (39, 539), (40, 540), (41, 541), (42, 542), (43, 543), (44, 544), (45, 545), (46, 546), (47, 547), (48, 548), (49, 549), (50, 550), (51, 551), (52, 552), (53, 553), (54, 554), (55, 555), (56, 556), (57, 557), (58, 558), (59, 559), (60, 560), (61, 561), (62, 562), (63, 563), (64, 564), (65, 565), (66, 566), (67, 567), (68, 568), (69, 569), (70, 570), (71, 571), (72, 572), (73, 573), (74, 574), (75, 575), (76, 576), (77, 577), (78, 578), (79, 579), (80, 580), (81, 581), (82, 582), (83, 583), (84, 584), (85, 585), (86, 586), (87, 587), (88, 588), (89, 589), (90, 590), (91, 591), (92, 592), (93, 593), (94, 594), (95, 595), (96, 596), (97, 597), (98, 598), (99, 599))'
>>> 

Upvotes: 0

Mike Graham
Mike Graham

Reputation: 76683

os.system expects a string which will will execute in the shell, but you're giving it a tuple instead.

Imagine we want to run the command rm -rf /home/mike. You might be doing something like

binary_and_option = 'rm -rf'
directory = '/home/mike'

command = binary_and_option, directory # This is the tuple 
                                       #    ('rm -rf', '/home/mike') 
                                       # it is NOT the string 
                                       #     'rm -rf /home/mike'

os.system(command) # this clearly won't work, since it's just 
                   # os.system(('rm -rf', '/home/mike')) 

what you want to do instead is

command = "%d %d" % (binary_and_option, directory)

to assemble the string. You are probably thinking comma assembles str-ed objects together with spaces in between, but that's only for print; it's not how strings work in general.


But wait, there's more! You never want to use os.system, especially when you're going to build commands. It invokes the shell (which introduces unncessary security risks and other penalties) and has an inflexible API. Instead, use the subprocess module.

import subprocess 

binary_and_option = ['rm', '-rf']
directory = '/home/mike'

command = binary_and_option + [directory]

subprocess.call(command)

Upvotes: 0

Katriel
Katriel

Reputation: 123632

What you've written is fine:

>>> x = 1
>>> y = 1, x
>>> 

The problem is that somewhere else in your code, you're using var_four as a string where it should be a tuple.

BTW, I think it's neater to put parentheses around tuples like this; otherwise I tend to think they're being used in tuple unpacking.


EDIT: There are all sorts of ways to join and format strings -- Python is good at that. In somewhat-decreasing order of generality:

"{first_thing} {second_thing}".format(first_thing=var_one, second_thing=var_two)

"{0} {1}".format(var_one, var_two)

var_one + var_two

Upvotes: 2

Edwin
Edwin

Reputation: 2114

Your code looks fine as is.

Try running import pdb; pdb.set_trace() in your program to see if you can find the line triggering the error.

EDIT: You'll want to use ''.join(var_four) to convert var_four into a string before adding it to whatever it is you want to use it. Please note that this will actually create a new string and not overwrite var_four. See Python 3 string.join() equivalent?

Also, you should be using the subprocess module instead of os.system. See the Python 3.x documentation.

Upvotes: 2

prelic
prelic

Reputation: 4518

one_big_string = ''.join(tuple)   
print one_big_string 

Upvotes: 4

Related Questions