- String conversion and formatting¶
- String Formatting
- Number formatting
- String .format() basics
- Substitution positioning
- Variable formatting
- Older % string formatter
- Formatted string literals
- Do math with f-strings:
- Call functions with f-strings;
- Delimiting f-strings
- F-String error
- Formatting tips with .format()
- Reuse same variable multiple times
- Convert values to different bases
- Use format as a function
- Internationalization
- Escaping braces
- Table formatting data
- Resources
String conversion and formatting¶
Functions for number conversion and formatted string output.
int PyOS_snprintf ( char * str , size_t size , const char * format , . ) ¶
Part of the Stable ABI .
Output not more than size bytes to str according to the format string format and the extra arguments. See the Unix man page snprintf(3).
int PyOS_vsnprintf ( char * str , size_t size , const char * format , va_list va ) ¶
Part of the Stable ABI .
Output not more than size bytes to str according to the format string format and the variable argument list va. Unix man page vsnprintf(3).
PyOS_snprintf() and PyOS_vsnprintf() wrap the Standard C library functions snprintf() and vsnprintf() . Their purpose is to guarantee consistent behavior in corner cases, which the Standard C functions do not.
The wrappers ensure that str[size-1] is always ‘\0’ upon return. They never write more than size bytes (including the trailing ‘\0’ ) into str. Both functions require that str != NULL , size > 0 , format != NULL and size < INT_MAX . Note that this means there is no equivalent to the C99 n = snprintf(NULL, 0, . ) which would determine the necessary buffer size.
The return value (rv) for these functions should be interpreted as follows:
- When 0 rv characters were written to str (excluding the trailing ‘\0’ byte at str[rv] ).
- When rv >= size , the output conversion was truncated and a buffer with rv + 1 bytes would have been needed to succeed. str[size-1] is ‘\0’ in this case.
- When rv < 0 , “something bad happened.” str[size-1] is '\0' in this case too, but the rest of str is undefined. The exact cause of the error depends on the underlying platform.
The following functions provide locale-independent string to number conversions.
double PyOS_string_to_double ( const char * s , char * * endptr , PyObject * overflow_exception ) ¶
Part of the Stable ABI .
Convert a string s to a double , raising a Python exception on failure. The set of accepted strings corresponds to the set of strings accepted by Python’s float() constructor, except that s must not have leading or trailing whitespace. The conversion is independent of the current locale.
If endptr is NULL , convert the whole string. Raise ValueError and return -1.0 if the string is not a valid representation of a floating-point number.
If endptr is not NULL , convert as much of the string as possible and set *endptr to point to the first unconverted character. If no initial segment of the string is the valid representation of a floating-point number, set *endptr to point to the beginning of the string, raise ValueError, and return -1.0 .
If s represents a value that is too large to store in a float (for example, «1e500» is such a string on many platforms) then if overflow_exception is NULL return Py_HUGE_VAL (with an appropriate sign) and don’t set any exception. Otherwise, overflow_exception must point to a Python exception object; raise that exception and return -1.0 . In both cases, set *endptr to point to the first character after the converted value.
If any other error occurs during the conversion (for example an out-of-memory error), set the appropriate Python exception and return -1.0 .
char * PyOS_double_to_string ( double val , char format_code , int precision , int flags , int * ptype ) ¶
Part of the Stable ABI .
Convert a double val to a string using supplied format_code, precision, and flags.
format_code must be one of ‘e’ , ‘E’ , ‘f’ , ‘F’ , ‘g’ , ‘G’ or ‘r’ . For ‘r’ , the supplied precision must be 0 and is ignored. The ‘r’ format code specifies the standard repr() format.
flags can be zero or more of the values Py_DTSF_SIGN , Py_DTSF_ADD_DOT_0 , or Py_DTSF_ALT , or-ed together:
- Py_DTSF_SIGN means to always precede the returned string with a sign character, even if val is non-negative.
- Py_DTSF_ADD_DOT_0 means to ensure that the returned string will not look like an integer.
- Py_DTSF_ALT means to apply “alternate” formatting rules. See the documentation for the PyOS_snprintf() ‘#’ specifier for details.
If ptype is non- NULL , then the value it points to will be set to one of Py_DTST_FINITE , Py_DTST_INFINITE , or Py_DTST_NAN , signifying that val is a finite number, an infinite number, or not a number, respectively.
The return value is a pointer to buffer with the converted string or NULL if the conversion failed. The caller is responsible for freeing the returned string by calling PyMem_Free() .
Case insensitive comparison of strings. The function works almost identically to strcmp() except that it ignores the case.
int PyOS_strnicmp ( const char * s1 , const char * s2 , Py_ssize_t size ) ¶
Case insensitive comparison of strings. The function works almost identically to strncmp() except that it ignores the case.
String Formatting
Python v2.7 introduced a new string formatting method, that is now the default in Python3. I started this string formatting cookbook as a quick reference to help me format numbers and strings. Thanks to other contributors I’ve expanded the examples over time.
Python 3.6 introduced, formatted string literals, often referred to as f-strings as another method to help format strings. It is simpler to prepend an f to the string then append .format() . Using f-strings in Python is similar to JavaScript’s template literals, if you are familiar with them.
Here’s an example comparing the three ways to format a float number:
pi = 3.14159 print(" pi = %1.2f " % pi) # older print(" pi = ".format( pi )) # .format() print(f" pi = pi:.2f>") # f-string
Number formatting
This table shows various ways to format numbers using Python’s str.format() and formatted string literals, including examples for both float formatting and integer formatting.
To run examples use: print(f»») or print(«».format(NUM));
Number | Format | Output | Description |
---|---|---|---|
3.1415926 | 3.14 | Format float 2 decimal places | |
3.1415926 | +3.14 | Format float 2 decimal places with sign | |
-1 | -1.00 | Format float 2 decimal places with sign | |
2.71828 | 3 | Format float with no decimal places | |
5 | 2d> | 05 | Pad number with zeros (left padding, width 2) |
5 | 5xxx | Pad number with x’s (right padding, width 4) | |
1000000 | 1,000,000 | Number format with comma separator | |
0.25 | 25.00% | Format percentage | |
1000000000 | 1.00e+09 | Exponent notation | |
13 | 13 | Right aligned (default, width 10) | |
13 | 13 | Left aligned (width 10) | |
13 | 13 | Center aligned (width 10) |
String .format() basics
Here are a couple of examples of basic string substitution, the <> is the placeholder for substituted variables. If no format is specified, it will insert and format as a string.
s1 = "show me the <>".format("money") s2 = "hmmm, this is a <> <>".format("tasty", "burger")
With formatted string literals, this is simply:
s1 = f"show me the money>" s2 = f"hmmm, this is a tasty> burger>"
Substitution positioning
One benefit of .format() that is not available in f-strings is using the numeric position of the variables and change them in the strings, this gives some flexibility when doing the formatting, if you make a mistake in the order you can easily correct without shuffling all the variables around.
s1 = " is better than ".format("emacs", "vim") s2 = " is better than ".format("emacs", "vim")
Variable formatting
You can use <> as a variable inside the formatting brackets (h/t Peter Beens for tip). This example uses a precision variable to control how many decimal places to show:
pi = 3.1415926 precision = 4 print( "<>f>".format( pi, precision ) ) >>> 3.1415
Older % string formatter
An example comparing variable substitution with the older % method vs. .format() :
s1 = "cats" s2 = "dogs" s3 = " %s and %s living together" % (s1, s2) s4 = " <> and <> living together ".format(s1, s2)
Using the older format method, I would often get the errors:
TypeError: not enough arguments for format string
TypeError: not all arguments converted during string formatting
because I miscounted my substitution variables, doing something like the following made it easy to miss a variable.
Using one of the new Python string formatters you can use numbered parameters so you don’t have to count how many you have, at least on half of it.
set = " ( , , , , , , , ) ".format(a,b,c,d,e,f,g)
Formatted string literals
As shown above, formatted string literals, or f-strings, use a shorter syntax making it easier and more template-like. F-strings also support functions inside of the brackets < >this allows you to:
Do math with f-strings:
print( f"Do math: 3 * 6 = 3 * 6>" ) >>> Do math: 3 * 6 = 18
Call functions with f-strings;
verb = "runs" print( f"The girl verb.upper()> quickly." ) >>> The girl RUNS quickly.
Delimiting f-strings
You can use f-strings using the three different type of quotation marks in Python, single, double, or triple quotes. The following will all output the same:
name = "Fred" print( f'name>' ) print( f"name>" ) print( f"""name>""" )
F-String error
The one thing you’ll want to be careful is mixing the two formats, if you try to use <> inside of an f-string, you will get the error:
SyntaxError: f-string: empty expression not allowed
Each set of brackets used in an f-string requires a value or variable.
Formatting tips with .format()
The format() function offers additional features and capabilities, here are a few useful tips and tricks to format strings in Python:
Reuse same variable multiple times
Using % to format requires a strict ordering of variables, the .format() method allows you to put them in any order as well as repeating for reuse.
"Oh , ! wherefore art thou ?".format("Romeo") >>> 'Oh Romeo, Romeo! wherefore art thou Romeo?'
Convert values to different bases
A surprising use, you can use the string format command to convert numbers to different bases. Use the letter in the formatter to indicate the number base: decimal, hex, octal, or binary.
This example formats the number 21 in each base:
" - - - ".format(21) >>> 21 - 15 - 25 - 10101
Use format as a function
You can use .format as a function to separate text and formatting from code. For example, at the beginning of your program include all your formats for later use.
## defining formats email_f = "Your email address was ".format ## use elsewhere print(email_f(email="bob@example.com"))
Hat tip to earthboundkids who provided this on reddit.
Using format as a function can be used to adjust formating by user preference.
## set user preferred format num_format = " ".format ## use elsewhere print(num_format(1000000))
Internationalization
To use locale specific formatting for numbers, you need to first set the locale, and then use the formating code n instead of d . For example, using commas or periods to separate thousands in numbers based on the user’s locale.
Here is an example, setting locale and formatting a number to display the proper separator:
import locale locale.setlocale(locale.LC_ALL, '') print(" ".format(1000000))
Escaping braces
If you need to use braces when using str.format() just double them up:
print(" The <> set is often represented as % raw %>>% endraw %>".format("empty")) ~~ The empty set is often represented as 0>
Table formatting data
Use the width and the left and right justification to align your data into a nice table format. Here’s an example to show how to format:
# data starters = [ [ 'Andre Iguodala', 4, 3, 7 ], [ 'Klay Thompson', 5, 0, 21 ], [ 'Stephen Curry', 5, 8, 36 ], [ 'Draymon Green', 9, 4, 11 ], [ 'Andrew Bogut', 3, 0, 2 ], ] # define format row row = "| | | | |".format for p in starters: print(row(player=p[0], reb=p[1], ast=p[2], pts=p[3]))
| Andre Iguodala | 4 | 3 | 7 | | Klay Thompson | 5 | 0 | 21 | | Stephen Curry | 5 | 8 | 36 | | Draymon Green | 9 | 4 | 11 | | Andrew Bogut | 3 | 0 | 2 |
Resources
- Python String Library – Standard Library Documentation
- My Python Argparse Cookbook – examples parsing command-line arguments
- My Python Date Formatting — examples working with Python dates.