Equivalent Operation to b"" for an already defined string Python [duplicate] - python

This question already has answers here:
Best way to convert string to bytes in Python 3?
(5 answers)
Closed 8 months ago.
The community reviewed whether to reopen this question 8 months ago and left it closed:
Original close reason(s) were not resolved
I am able to define a bytes string as follows:
string = b"0x340xEF"
I want to cast an already made string. Let us call it string into a bytes string. How do I perform this? bytes(string) is not giving me an equivalent result.

You need to encode the string to be able to convert it to bytes:
string = "b0x340xEF".encode("latin-1")
print(string)
Output:
b'b0x340xEF'

Related

rstrip() method for strings [duplicate]

This question already has answers here:
How do I remove a substring from the end of a string?
(23 answers)
Closed 6 months ago.
The community reviewed whether to reopen this question 6 months ago and left it closed:
Original close reason(s) were not resolved
Can someone please do this? Shouldn't the print command print There is a space. Here it is skipping as well while it should not. TIA
The argument of the method rstrip(characters) is a string of characters that will be removed from the right. That means, that the right-most char in the some_sentence string is in the set characters, it will be removed. And that's done until the right-most char is not in the set to be removed.
See https://docs.python.org/3/library/stdtypes.html?highlight=rstrip#str.rstrip
What you want to accomplish is done with .removesuffix()

Changing string to ascii in python [duplicate]

This question already has answers here:
Convert a Unicode string to a string in Python (containing extra symbols)
(12 answers)
Closed 3 years ago.
I need to convert word
name = 'Łódź'
to ASCII characters
output: 'Lodz'
I can't import any library like unicodedata.
I need to do it in clear python.
I've tried to encode than decode and nothing worked.
Well, a simple method would be to map and replace. This also does not require any special imports.
name = 'Łódź'
name=name.replace('Ł','L')
name=name.replace('ó','o')
name=name.replace('ź','z')
print(name)

Decode part literal bytes in str [duplicate]

This question already has answers here:
Converting double slash utf-8 encoding
(6 answers)
Closed 4 years ago.
I have a str like "abc\\xe2\\x86\\x92", what I want is "abc→". (b"abc\xe2\x86\x92".encode() equals →)
The problem is, \\xe2\\x86\\x92 is in str, and \\ is just one character so there is no way to replace it into \.
I found a way to solve this:
In [107]: original.encode().decode('unicode-escape').encode('latin1').decode('utf-8')
Out[107]: 'abc→'

Python 3 - Hex-String to bytes [duplicate]

This question already has an answer here:
Python: Converting HEX string to bytes
(1 answer)
Closed 2 years ago.
I am using python 3 and try to convert a hex-string to a byte-represented form. So i used the following command:
bytes.fromhex('97ad300414b64c')
I Expected results like this: b'\x97\xad\x30\x04\x14\xb6\x4c'' but got b'\x97\xad0\x04\x14\xb6L'. I am note sure what i am doing wrong, but maybe it is something with the encoding?
As pointed by #user8651755 in the comments, this is due to the fact that some bytes correspond to printable characters. So the answer is: you are doing everything right.

Converting non-digit strings in Python [duplicate]

This question already has answers here:
Is there a way to convert number words to Integers?
(17 answers)
Closed 5 years ago.
What is the best way to parse non-digit strings e.g. "twenty two"->22 to integer or double values in python ? Especially what is the best way to detect that region if there are additional words in a sentence.
Thanks
https://github.com/akshaynagpal/w2n this has pip install
Original Comment here
https://stackoverflow.com/a/34569447

Categories