Converting int to String
00:01
Converting a Python int to a string. In Python, you can convert a Python int to a string using str()
, as seen here. As you can see, i
is an integer and we can obtain the string representation, as seen here.
00:25
You can see that i
is of <class 'int'>
. and the string representation is indeed a string. By default, this behaves like int()
in that it results in a decimal representation.
00:45 In this example, it’s smart enough to interpret the binary literal and convert it to a decimal string. If you want a string to represent an integer in another number system, then you can use a formatted string such as an f-string in Python 3.6 and later, and an option that specifies the base as seen here.
01:08 So as you can see, the octal variable represents 571 in decimal, and we can specify a different base, as seen here.
01:21
Using the :x
representation has converted to hexadecimal. And using :b
has converted the number to binary. This gives a flexible way to represent an integer in a variety of different number systems.
Become a Member to join the conversation.