In this tutorial, we will look at how to convert an octal number (as a string) to an integer in Python with the help of examples.
How to convert octal string to integer in Python?
You can use the Python built-in int()
function to convert an octal string to an integer in Python. Pass the oct string as an argument. The following is the syntax –
# oct string to int int(oct_str, 8)
The int()
function also takes an optional argument, base
to identify the base of the value passed. For example, use 16
as the base to convert hex string, use 8
as the base for an octal string, etc.
Although, the int()
function is capable of inferring the base from the string passed, it’s a good practice to explicitly specify the base.
Examples
Let’s look at some examples of using the above syntax to convert an oct string to an int.
Oct string with prefix to int
Let’s convert an octal number (as a string) with the prefix '0o'
to an integer using the int()
function.
# oct string representing 1024 oct_str = '0o2000' # oct to int num = int(oct_str, 8) print(num)
Output:
1024
Here we have an oct string representing the number 1024
with the prefix '0o'
. We use the int()
function to convert the oct string to an integer. You can see that we get 1024
as the output.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Oct string without prefix to int
Let’s now try to convert an oct string without a prefix to int using the int()
function. You don’t need to change any arguments as the int()
function is capable to handle the oct string irrespective of the presence of the prefix.
# oct string representing 1024 without '0o' prefix oct_str = '2000' # oct to int num = int(oct_str, 8) print(num)
Output:
1024
Here we pass the oct string without the '0o'
prefix to the int()
function with base
as 8. We get the same output as above.
For more on the Python int()
function, refer to its documentation.
You might also be interested in –
- Python – Convert Hexadecimal String to Integer
- Convert int to bytes in Python
- Python – Convert Integer to Binary String
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.