12-08-2022, 01:23 PM
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!![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
[i]This is you and your friend celebrating after having solved this problem!
[/i]
What Is An Octal Escape Sequence?![[Image: 1f4a1.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png)
An Octal Escape Sequence is a backslash followed by 1-3 octal digits () such as which encodes the ASCII character . Each octal escape sequence encodes one character (except invalid octal sequence ). You can chain together multiple octal escape sequences to obtain a word.
Problem Formulation
Question: How to convert an octal escape sequence to a string and vice versa in Python?
Examples
Octal |
String
|
|
|
|
|
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:
Python Octal to String Explicit Conversion
The function converts a given object represented by an (octal) escape sequence to a Python string. For example, yields the Unicode (string) character .
Example: Convert the octal representations presented above:
This leads to the following expected output:
Python String to Octal
To convert a Python string to an octal escape sequence representation, iterate over each character and convert it to an octal escape sequence using .
The result uses octal representation such as. You can do string manipulation, such as slicing and string concatenation, to bring it into the final version , for instance.
Here’s the function that converts string to octal escape sequence format:
Background:
Let’s check how our strings can be converted to the octal escape sequence representation using this function:
And here’s the expected output:
If you don’t like the one-liner solution provided above, feel free to use this multi-liner instead that may be easier to read:
If you want to train your Python one-liner skills instead, check out my book!![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
Python One-Liners Book: Master the Single Line First!
Python programmers will improve their computer science skills with these useful one-liners.
![[Image: 3D_cover-1024x944.jpg]](https://blog.finxter.com/wp-content/uploads/2020/06/3D_cover-1024x944.jpg)
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!!
5/5 – (1 vote)
This tutorial will show you how to convert an
- octal escape sequence to a Python string, and a
- Python string to an octal escape sequence.
But let’s quickly recap what an octal escape sequence is in the first place!
![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
[i]This is you and your friend celebrating after having solved this problem!
[/i]What Is An Octal Escape Sequence?
![[Image: 1f4a1.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png)
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:- The function takes a character (=string of length one) as an input and returns the Unicode number of this character. For example,Code:
ord()returns the Unicode numberCode:ord('a'). The inverse function ofCode:97is theCode:ord()function, soCode:chr()returns the original characterCode:chr(ord('a')).Code:'a'
- The function takes one integer argument and returns an octal string with prefixCode:
oct().Code:"0o"
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!
![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
Python One-Liners Book: Master the Single Line First!
Python programmers will improve their computer science skills with these useful one-liners.
![[Image: 3D_cover-1024x944.jpg]](https://blog.finxter.com/wp-content/uploads/2020/06/3D_cover-1024x944.jpg)
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:
- Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
- Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics
- Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
- Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators
- Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting
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!!

