Conversion

Sooner or later you're going to need to convert from one type to another. You may be reading in strings from files and want to convert them to text, or, before writing numbers to a text file, you may want to convert them to strings. Python has several built-in functions to do this.

Converting Numbers to Strings: chr(), str(), hex(), oct()

The chr() function converts a number to a character. Remember that there are no characters in Python, so chr() returns a single character in a string.

>>> chr(97)
'a'
>>> chr(122)
'z'

It may seem that you would rarely use chr(), but wait until Chapter 12, when it will come in handy for converting a Java binary array into a Python string.

The str() function converts any object, including numeric ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.