How to remove ) ' and , in python 2.7?

Viewed 104

Here is the code.

Password = "Here is your password: ", UpperCase,RandomNum
print(Password)

The result is something like this.

('Here is your password: ', 'Ajar', 98)

I have tried this.

print(Password).replace(",","")

But i get this error messege.

Traceback (most recent call last):
  File "/Users/Krun2810/PycharmProjects/pythonProject1/main.py", line 59, in <module>
    print(Password.replace(",",""))
AttributeError: 'tuple' object has no attribute 'replace'

How do I remove ,, ) and ` from the result?

4 Answers

You could do

Password = "Here is your password: " + str(UpperCase) + str(RandomNum)
Password = "Here is your password: ", UpperCase,RandomNum

On this line you're actually making a tuple instead of making a string! Instead of a ',' character try a + character.

Consider replacing this line:

Password = "Here is your password: ", UpperCase,RandomNum

with this line:

Password = "Here is your password: " + UpperCase + str(RandomNum)

Simply you can do something like this.

UpperCase, RandomNum = "Ajay", 98

Password = "Here is your password: %s %s" %(UpperCase,RandomNum)
print(Password)

output

'Here is your password: Ajay 98'

What you have there is a tuple with 3 elements. When you print it, it will display the string representation of that particular tuple. As the other answers suggest, one solution would be to concatenate the desired strings into one string variable, and print it.

pwd = "Here is your password: " + str(UpperCase) + str(RandomNum)
print(pwd)

where UpperCase and RandomNum are your variables.

Related