wilberox
wilberox

Reputation: 193

how to remove a specific character from a tuple using python

I have a tuple:

tuple = ('one', ('two', 'three'))

I want to print remove the ' from this tuple so I can print it as (one, (two, three)).

I have tried this

tuple = str(tuple)
print tuple.strip(')

But I get this error:

    print tuple.strip(')
                       ^
SyntaxError: EOL while scanning string literal

How can I turn my tuple into the desired string?

Upvotes: 1

Views: 462

Answers (2)

oppressionslayer
oppressionslayer

Reputation: 7224

You can print with replace:

print(str(tuple).replace("'", ''))

output

(one, (two, three))

Upvotes: 4

sammy
sammy

Reputation: 867

t = ('one', ('two', 'three'))
t2 = str(t)       
print(t2.replace("'","")) 

It's considered bad practice to use keywords like tuple as variable names, you might run into trouble with that.

Upvotes: 4

Related Questions