@@ -202,6 +202,32 @@ write code that handles both IP versions correctly. Address objects are
202
202
.. _iana-ipv4-special-registry : https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
203
203
.. _iana-ipv6-special-registry : https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml
204
204
205
+ .. method :: IPv4Address.__format__(fmt)
206
+
207
+ Returns a string representation of the IP address, controlled by
208
+ an explicit format string.
209
+ *fmt * can be one of the following: ``'s' ``, the default option,
210
+ equivalent to :func: `str `, ``'b' `` for a zero-padded binary string,
211
+ ``'X' `` or ``'x' `` for an uppercase or lowercase hexadecimal
212
+ representation, or ``'n' ``, which is equivalent to ``'b' `` for IPv4
213
+ addresses and ``'x' `` for IPv6. For binary and hexadecimal
214
+ representations, the form specifier ``'#' `` and the grouping option
215
+ ``'_' `` are available. ``__format__ `` is used by ``format ``, ``str.format ``
216
+ and f-strings.
217
+
218
+ >>> format (ipaddress.IPv4Address(' 192.168.0.1' ))
219
+ '192.168.0.1'
220
+ >>> ' {:#b } ' .format(ipaddress.IPv4Address(' 192.168.0.1' ))
221
+ '0b11000000101010000000000000000001'
222
+ >>> f ' { ipaddress.IPv6Address(" 2001:db8::1000" ):s } '
223
+ '2001:db8::1000'
224
+ >>> format (ipaddress.IPv6Address(' 2001:db8::1000' ), ' _X' )
225
+ '2001_0DB8_0000_0000_0000_0000_0000_1000'
226
+ >>> ' {: #_n} ' .format(ipaddress.IPv6Address(' 2001:db8::1000' ))
227
+ '0x2001_0db8_0000_0000_0000_0000_0000_1000'
228
+
229
+ .. versionadded :: 3.9
230
+
205
231
206
232
.. class :: IPv6Address(address)
207
233
@@ -246,8 +272,8 @@ write code that handles both IP versions correctly. Address objects are
246
272
groups consisting entirely of zeroes included.
247
273
248
274
249
- For the following attributes, see the corresponding documentation of the
250
- :class: `IPv4Address ` class:
275
+ For the following attributes and methods , see the corresponding
276
+ documentation of the :class: `IPv4Address ` class:
251
277
252
278
.. attribute :: packed
253
279
.. attribute :: reverse_pointer
@@ -297,6 +323,12 @@ write code that handles both IP versions correctly. Address objects are
297
323
the embedded ``(server, client) `` IP address pair. For any other
298
324
address, this property will be ``None ``.
299
325
326
+ .. method :: IPv6Address.__format__(fmt)
327
+
328
+ Refer to the corresponding method documentation in
329
+ :class: `IPv4Address `.
330
+
331
+ .. versionadded :: 3.9
300
332
301
333
Conversion to Strings and Integers
302
334
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0 commit comments