In Python, the values that "are inside" the variables (technically "associated with names") are all objects - just as the literals typed directly in the program are also objects. Literals are, for example, what you’re calling a string - all the values that are typed in the program - be b"maca"
, numbers like 123.45
, etc.....
For language, when we use a method of an object type, whether the object was typed directly into the program code, or a literal, or whether it is in a variable.
That is to say:
meus_bytes = b"frase de teste"
minha_string = meus_bytes.decode("utf-8")
- The method
decode
with the notation of .
, is called with the . after the variable name, and works in the same way as the .
after the literal in b"frase_de_Teste".decode()
.
A related but little known trick is that even methods associated with numbers - both floating point and integers, can be called straight from literals - but in this case at least a blank space is required separating the digits from the numbers, to differentiate access attributes from decimal point - for example: 1234 .to_bytes(2, "little")
What do you mean by that? Like having that name as a variable? Try using
eval()
andexec()
.– Breno
what I wanted to do is this for example; b "abcde". Decode("utf-8") and put in "abcde" a variable and make the conversion
– Pedro Pinheiro
Your question makes no sense... If the variable contains bytes, just use the Decode method normally. Type:
meus_bytes = b'Ola'
afterwardmeus_bytes.decode("utf-8")
...– fernandosavio