![]() |
|
[Tut] How to Convert an Octal Escape Sequence in Python – And Vice Versa? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Convert an Octal Escape Sequence in Python – And Vice Versa? (/thread-100365.html) |
[Tut] How to Convert an Octal Escape Sequence in Python – And Vice Versa? - xSicKxBot - 12-08-2022 How to Convert an Octal Escape Sequence in Python – And Vice Versa? 5/5 – (1 vote) This tutorial will show you how to convert an
But let’s quickly recap what an octal escape sequence is in the first place! ![]() [i]This is you and your friend celebrating after having solved this problem! [/i]What Is An Octal Escape Sequence? ![]() An Octal Escape Sequence is a backslash followed by 1-3 octal digits ( Code: 0-7Code: \150Code: 'h'Code: \000Problem Formulation Question: How to convert an octal escape sequence to a string and vice versa in Python?Examples Octal | String Code: \101 \102 \103Code: 'ABC'Code: \101 \040 \102 \040 \103Code: 'A B C'Code: \141 \142 \143Code: 'abc'Code: \150 \145 \154 \154 \157Code: 'hello'Code: \150 \145 \154 \154 \157 \040 \167 \157 \162 \154 \144Code: 'hello world'Python Octal to String Built-In Conversion You don’t need to “convert” an octal escape sequence to a Unicode string if you already have it represented by a bytes object. Python automatically resolves the encoding. See here: Code: >>> b'\101\102\103'
b'ABC'
>>> b'101\040\102\040\103'
b'101 B C'
>>> b'\101\040\102\040\103'
b'A B C'
>>> b'\141\142\143'
b'abc'
>>> b'\150\145\154\154\157'
b'hello'
>>> b'\150\145\154\154\157\040\167\157\162\154\144'
b'hello world'The Code: bytes.decode('unicode-escape')Code: bytesCode: br'\101'.decode('unicode-escape')Code: 'A'Code: def octal_to_string(x): ''' Converts an octal escape sequence to a string''' return x.decode('unicode-escape')Example: Convert the octal representations presented above: Code: octals = [br'\101\102\103', br'\101\040\102\040\103', br'\141\142\143', br'\150\145\154\154\157', br'\150\145\154\154\157\040\167\157\162\154\144'] for octal in octals: print(octal_to_string(octal))This leads to the following expected output: Code: ABC
A B C
abc
hello
hello worldPython String to Octal To convert a Python string to an octal escape sequence representation, iterate over each character Code: cCode: oct(ord(c))The result uses octal representation such as Code: '0o123'Code: '\123'Here’s the function that converts string to octal escape sequence format: Code: def string_to_octal(x): ''' Converts a string to an octal escape sequence''' return '\\' + '\\'.join(oct(ord(c))[2:] for c in x) Background:
Let’s check how our strings can be converted to the octal escape sequence representation using this function: Code: strings = ['ABC', 'A B C', 'abc', 'hello', 'hello world'] for s in strings: print(string_to_octal(s))And here’s the expected output: Code: \101\102\103
\101\40\102\40\103
\141\142\143
\150\145\154\154\157
\150\145\154\154\157\40\167\157\162\154\144If you don’t like the one-liner solution provided above, feel free to use this multi-liner instead that may be easier to read: Code: def string_to_octal(x): ''' Converts a string to an octal escape sequence''' result = '' for c in x: result += '\\' + oct(ord(c))[2:] return resultIf you want to train your Python one-liner skills instead, check out my book! ![]() Python One-Liners Book: Master the Single Line First! Python programmers will improve their computer science skills with these useful one-liners. ![]() Python One-Linerswill teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert. The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms. Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments. You’ll also learn how to:
By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line. Get your Python One-Liners on Amazon!! |