Anonymous user
Binary strings: Difference between revisions
→{{header|Python}}
m (→{{header|Icon}} and {{header|Unicon}}: -workswith) |
|||
Line 957:
=={{header|Python}}==
===2.x===
Python
* String creation
<lang python>s1 = "A 'string' literal \n"
Line 966 ⟶ 967:
goes over several lines
up to the closing triple quote"""</lang>
* String assignment
Line 988 ⟶ 987:
* String cloning and copying
Strings are immutable, so there is no need to clone/copy them. If you want to modify a string, you must create a new one with the desired contents. (There is another type, ''array'', that provides a mutable buffer
* Check if a string is empty
Line 1,049 ⟶ 1,048:
# output:
# ['Smith', 'John', '417 Evergreen Av', 'Chimichurri', '481-3172']</lang>
===3.x===
Python 3.x has two binary string types: <code>bytes</code> (immutable) and <code>bytearray</code> (mutable). They can contain any byte sequence. They are completely separate from the string type (<code>str</code>). Most of the operators for strings, also work on <code>bytes</code> and <code>bytearray</code>
To specify a literal immutable byte string (<code>bytes</code>), prefix a string literal with "b":
<lang python>s1 = b"A 'byte string' literal \n"
s2 = b'You may use any of \' or " as delimiter'
s3 = b"""This text
goes over several lines
up to the closing triple quote"""</lang>
You can use the normal string escape sequences to encode special bytes.
Indexing a byte string results in an integer (the byte value at that byte):
<lang python>x = b'abc'
x[0] # evaluates to 97</lang>
Similarly, a byte string can be converted to and from a list of integers:
<lang python>x = b'abc'
list(x) # evaluates to [97, 98, 99]
bytes([97, 98, 99]) # evaluates to b'abc'</lang>
=={{header|REXX}}==
|