O'Reilly logo

Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython by Richard Hightower

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required