Python:Convert a Number to a String

From Progzoo
Jump to: navigation, search

The easy way to do this is to use the str() function

The powerful way to do this is to use the % string formatting operator

Number to String by str

[Font] [Default] [Show] [Resize] [History] [Profile]

Creating a string

The % operator takes a format string and an argument or tuple of arguments. The format string includes %d spaces where the integers should go or %f places for the floating point numbers.

We can specify the total number of characters to use (the number is right justified in this space). We can specify the number of decimal places to use.

[Font] [Default] [Show] [Resize] [History] [Profile]