-2
from cryptography.fernet import Fernet
from test2 import display

file = 'Fernet'

message = display


key = Fernet.generate_key()
fernet = Fernet(key)
encMessage = fernet.encrypt(message.encode())
decMessage = fernet.decrypt(encMessage)


print("key: ", key)
print("Original string: ", message)
print(("encrypted string: ", encMessage))
# noinspection PyTypeChecker
print(f'decrypted string: =\'[decMessage.rstrip(b'message)]')])

I am not able to remove b' from the decrypted string i tried using rstrip, always get SyntaxError: invalid syntax

Shadow
  • 1
  • 1
  • You cannot "remove" `b'` because it is not even part of the string, it is how python decides to display a binary string. It is like asking how to remove the `.` from the number `1.2`. – luk2302 Jun 28 '22 at 09:28
  • Do you want to remove only the **trailing b** from the right or do you want to remove all the **b** in the string [solution for both](https://stackoverflow.com/a/72784115/16177121) – Sidharth Mudgil Jun 28 '22 at 09:53

2 Answers2

0

rstrip() is for removing white space on the right side of the string.

You've got a syntax error because your quotation marks aren't set correctly. As you can see, rstrip() in your code is marked green which indicates that the code thinks that that is part of the string.

Please check out the following material.

rstrip - https://www.w3schools.com/python/ref_string_rstrip.asp

f-strings - https://realpython.com/python-f-strings/

Erxuan Li
  • 76
  • 3
0

You're using the wrong syntax for F-String as well as rstrip, Also the quotations are also uneven. Use single quotation inside double quotation i.e. "''" to prevent using escape symbols i.e. '\'\''.

  • You've to put expression inside the curly-braces { } for String formatting. learn more
  • correct syntax: myString.rstrip(myChar) learn more

Correct Way

decMessage = "messagebbbb"
print(f"decrypted string: = '{decMessage.rstrip('b')}'")
>>> decrypted string: = 'message'

If you want to remove all the b's from the string you can replace b with '' using replace function

decMessage = "mbbesbsagebbbb"
print(f"decrypted string: = '{decMessage.replace('b', '')}'")
>>> decrypted string: = 'message'
Sidharth Mudgil
  • 1,293
  • 8
  • 25