Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > de.comp.lang.python > #6098
| From | Andreas Röhler <andreas.roehler@online.de> |
|---|---|
| Newsgroups | de.comp.lang.python |
| Subject | [Python-de] Re: Zuweisung via return |
| Date | 2024-07-04 13:43 +0200 |
| Message-ID | <8dcbd49d-7eda-433e-8ff8-c49115873833@online.de> (permalink) |
| References | <7324215d-fbce-490c-97de-f0a825e85780@online.de> <e8bc2a6e-b67e-4a5a-a215-cb03cb17f32b@crazy-compilers.com> |
Am 03.07.24 um 11:14 schrieb Hartmut Goebel:
> Am 02.07.24 um 20:56 schrieb Andreas Röhler:
>> print("zaehler: {}".format(zaehler))
>
> Immer wieder sehe ich diese unnötigen und ineffizienten
> String-Formatierungen bei "print".
>
> print("zaehler:", zaehler)
>
> tut genau das gleiche.
>
In diesem Fall schon. Aber warum für jede Aufgabe eine neue Form verwenden?
Nach meinem Dafürhalten ist, was "Format Specification Mini-Language"
anbietet, ungleich mächtiger - und am Ende auch einfacher.
Hab mal ein paar Beispiele herausgesucht:
# python/python-3.10.13-docs-html/library/string.html#formatspec
# Accessing arguments by position:
print('{0}{1}{0}'.format('abra', 'cad'))
# >>> >>> abracadabra
# Accessing arguments by name:
print('Coordinates: {latitude}, {longitude}'.format(latitude='37.24N',
longitude='-115.81W'))
# >>> Coordinates: 37.24N, -115.81W
# Replacing %s and %r:
print("repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1',
'test2'))
# >>> repr() shows quotes: 'test1'; str() doesn't: test2
# Accessing arguments’ attributes:
c = 3-5j
print(('The complex number {0} is formed from the real part {0.real} '
'and the imaginary part {0.imag}.').format(c))
# >>> The complex number (3-5j) is formed from the real part 3.0 and the
imaginary part -5.0.
# Using type-specific formatting:
import datetime
d = datetime.datetime(2010, 7, 4, 12, 15, 58)
print('{:%Y-%m-%d %H:%M:%S}'.format(d))
# >>> 2010-07-04 12:15:58
Back to de.comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar
[Python-de] Zuweisung via return Andreas Röhler <andreas.roehler@online.de> - 2024-07-02 20:56 +0200
Re: [Python-de] Zuweisung via return "Peter J. Holzer" <hjp-usenet4@hjp.at> - 2024-07-02 23:02 +0200
[Python-de] Re: Zuweisung via return Andreas Röhler <andreas.roehler@online.de> - 2024-07-03 16:28 +0200
Re: [Python-de] Re: Zuweisung via return "Peter J. Holzer" <hjp-usenet4@hjp.at> - 2024-07-03 16:36 +0200
Re: [Python-de] Re: Zuweisung via return Hermann Riemann <nospam.ng@hermann-riemann.de> - 2024-07-03 21:14 +0200
Re: [Python-de] Re: Zuweisung via return "Peter J. Holzer" <hjp-usenet4@hjp.at> - 2024-07-04 00:09 +0200
Re: [Python-de] Zuweisung via return Marcel Logen <333200007110-0201@ybtra.de> - 2024-07-03 00:09 +0200
Re: [Python-de] Zuweisung via return "Peter J. Holzer" <hjp-usenet4@hjp.at> - 2024-07-03 10:39 +0200
[Python-de] Re: Zuweisung via return Andreas Röhler <andreas.roehler@online.de> - 2024-07-03 11:21 +0200
Re: [Python-de] Zuweisung via return Stephan Herrmann <stephan.herrmann@mailbox.org> - 2024-07-03 00:22 +0200
[Python-de] Re: Zuweisung via return Hartmut Goebel <h.goebel@crazy-compilers.com> - 2024-07-03 11:14 +0200
Re: [Python-de] Re: Zuweisung via return "Peter J. Holzer" <hjp-usenet4@hjp.at> - 2024-07-03 13:43 +0200
[Python-de] Re: Zuweisung via return Hartmut Goebel <h.goebel@crazy-compilers.com> - 2024-07-03 14:41 +0200
Re: [Python-de] Re: Zuweisung via return "Peter J. Holzer" <hjp-usenet4@hjp.at> - 2024-07-03 15:40 +0200
[Python-de] Re: Zuweisung via return Andreas Röhler <andreas.roehler@online.de> - 2024-07-04 13:43 +0200
Re: [Python-de] Re: Zuweisung via return Hermann Riemann <nospam.ng@hermann-riemann.de> - 2024-07-05 05:47 +0200
[Python-de] Re: Zuweisung via return Hartmut Goebel <h.goebel@crazy-compilers.com> - 2024-07-05 10:48 +0200
Re: Zuweisung via return Hermann Riemann <nospam.ng@hermann-riemann.de> - 2024-07-05 16:08 +0200
[Python-de] Re: Zuweisung via return Andreas Röhler <andreas.roehler@online.de> - 2024-07-03 14:02 +0200
[Python-de] Re: Zuweisung via return Frank Grellert <grellert@web.de> - 2024-07-03 23:43 +0200
[Python-de] Re: Zuweisung via return Andreas Röhler <andreas.roehler@online.de> - 2024-07-04 13:35 +0200
csiph-web