2

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?

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
Krun
  • 29
  • 1
  • 4
    Python 2.7 [reached EOL at the start of 2020](https://endoflife.date/python). Strongly consider upgrading to Python 3. – costaparas Feb 06 '21 at 13:12
  • 2
    the result = ('Here is your password: ', 'Ajar', 98) which is not a string but as the error suggests is a tuple. You cannot do a replace on a tuple but only on a string. First you would need join all elements of your tuple into a string and then you could do the replace – Matt Feb 06 '21 at 13:15
  • 1
    Does this answer your question? [How to transform a tuple to a string of values without comma and parentheses](https://stackoverflow.com/q/17426386/2745495) – Gino Mempin Feb 06 '21 at 13:16
  • `Password = UpperCase + str(RandomNum); print "Here is your password:", Password` –  Feb 06 '21 at 13:16
  • How this question was upvoted 3 points is beyond my understanding ! – Iron Fist Feb 06 '21 at 14:06

4 Answers4

2

You could do

Password = "Here is your password: " + str(UpperCase) + str(RandomNum)
1
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)
mcatee
  • 41
  • 4
1

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'
Ali Aref
  • 1,893
  • 12
  • 31
0

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.