What Does the B in Front of String Literals Do

What does the 'b' character do in front of a string literal?

To quote the Python 2.x documentation:

A prefix of 'b' or 'B' is ignored in
Python 2; it indicates that the
literal should become a bytes literal
in Python 3 (e.g. when code is
automatically converted with 2to3). A
'u' or 'b' prefix may be followed by
an 'r' prefix.

The Python 3 documentation states:

Bytes literals are always prefixed with 'b' or 'B'; they produce an instance of the bytes type instead of the str type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes.

What does the b in front of string literals do?

This is a forward compatibility token for the never-to-be-released PHP version 6, which should have had native unicode support.

In PHP6, strings are unicode by default, and functions operate at the unicode character level on them. This "b" means "binary string", that is, a non unicode string, on which functions operate at the byte level.

This has no effect in PHP != 6, where all strings are binary.

What does a b prefix before a python string mean?

This is Python3 bytes literal. This prefix is absent in Python 2.5 and older (it is equivalent to a plain string of 2.x, while plain string of 3.x is equivalent to a literal with u prefix in 2.x). In Python 2.6+ it is equivalent to a plain string, for compatibility with 3.x.

How do I get rid of the b-prefix in a string in python?

decode the bytes to produce a str:

b = b'1234'
print(b.decode('utf-8')) # '1234'

Remove 'b' character do in front of a string literal in Python 3

Decoding is redundant

You only had this "error" in the first place, because of a misunderstanding of what's happening.

You get the b because you encoded to utf-8 and now it's a bytes object.

 >> type("text".encode("utf-8"))
>> <class 'bytes'>

Fixes:

  1. You can just print the string first
  2. Redundantly decode it after encoding

What does preceding a string literal with r mean?

The r means that the string is to be treated as a raw string, which means all escape codes will be ignored.

For an example:

'\n' will be treated as a newline character, while r'\n' will be treated as the characters \ followed by n.

When an 'r' or 'R' prefix is present,
a character following a backslash is
included in the string without change,
and all backslashes are left in the
string. For example, the string
literal r"\n" consists of two
characters: a backslash and a
lowercase 'n'. String quotes can be
escaped with a backslash, but the
backslash remains in the string; for
example, r"\"" is a valid string
literal consisting of two characters:
a backslash and a double quote; r"\"
is not a valid string literal (even a
raw string cannot end in an odd number
of backslashes). Specifically, a raw
string cannot end in a single
backslash (since the backslash would
escape the following quote character).
Note also that a single backslash
followed by a newline is interpreted
as those two characters as part of the
string, not as a line continuation.

Source: Python string literals

np.genfromtxt returns string with 'b'

The answer is that b before strings means that it is a byte object normally returned with utf-8 encoding. It is a bytes object.

To remove it, there is a parameter in genfromtxt that is encoding, set it to utf-8

i.e

df3 = np.genfromtxt('100 Sales Records.csv', delimiter=',',names=True, dtype=None, encoding='utf-8')

This will give you the desired results.

What does the @ prefix do on string literals in C#

@ is not related to any method.

It means that you don't need to escape special characters in the string following to the symbol:

@"c:\temp"

is equal to

"c:\\temp"

Such string is called 'verbatim' or @-quoted. See MSDN.



Related Topics



Leave a reply



Submit