Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > de.comp.lang.python > #5647

Re: [Python-de] f-string Formatanweisung

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Stefan Behnel <python-de@behnel.de>
Newsgroups de.comp.lang.python
Subject Re: [Python-de] f-string Formatanweisung
Date Mon, 22 Jun 2020 15:14:47 +0200
Lines 49
Message-ID <mailman.253.1592831688.24731.python-de@python.org> (permalink)
References <rcq1j2$5b5$1@dont-email.me> <3e3c30fe-d11e-6061-2ed4-0a7015359882@behnel.de>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Trace news.uni-berlin.de YnKjCvwYZaV/oivKy+z+wwxNhCAHQ2op97MJqfeIrpQg==
Return-Path <python-de@behnel.de>
X-Original-To python-de@python.org
Delivered-To python-de@mail.python.org
Authentication-Results mail.python.org; dkim=pass reason="2048-bit key; unprotected key" header.d=behnel.de header.i=@behnel.de header.b=JxEdSgIM; dkim-adsp=pass; dkim-atps=neutral
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; t=1592831687; s=strato-dkim-0002; d=behnel.de; h=In-Reply-To:Date:Message-ID:References:To:Subject:From: X-RZG-CLASS-ID:X-RZG-AUTH:From:Subject:Sender; bh=0hse+Az+jkNfx70+vuBH2BKnjYnrf30o68tiQizhHYE=; b=JxEdSgIMxPwBEWkAr78S4r9e7FFtvI5y9Wym36Akwhd81UHiRrCV92f/+ZV6fUKBuE Q8GJ04ziSHd874J5ytealckNdrrbcqm5CmbmX/68BdaZb/SfzMW6rKB6pIVVRcdwRVVd 4J6i5Qbd9ti5nGIpTie8BdjMdPh6697/7z5wdvRJle6+RHDk7dS7W7BkbdWqDe48QS6p QpwA/RexH2QHV0MXTTWKQjIw421dQkXCXGvbs4DU/L11a1CapwnzaCt9QRLbC4qvv5wc mpsSVy6GBpCHxpTfzrkIem1gbuPUH85PdNIIyxRsf/EtvGR2UJtms0qyDQKHk+MSaQDA bLOg==
X-RZG-AUTH ":E1MMdFW4b++AXZOTwA41DOYM0Dv9LNWvavC/fJZ6Wfgmp/Lh1ANWCRaaq2R1hHsoZh74ArcRjqdrtbnfdSk7SR00Qivwavc27kbrx2CHRUeo5xXR"
X-RZG-CLASS-ID mo00
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0
In-Reply-To <rcq1j2$5b5$1@dont-email.me>
Content-Language de-DE
X-BeenThere python-de@python.org
X-Mailman-Version 2.1.33
Precedence list
List-Id Die Deutsche Python Mailingliste <python-de.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-de>, <mailto:python-de-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-de/>
List-Post <mailto:python-de@python.org>
List-Help <mailto:python-de-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-de>, <mailto:python-de-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <3e3c30fe-d11e-6061-2ed4-0a7015359882@behnel.de>
X-Mailman-Original-References <rcq1j2$5b5$1@dont-email.me>
Xref csiph.com de.comp.lang.python:5647

Show key headers only | View raw


Hallo nochmal,

es hilft doch durchaus, nicht nur Code zu lesen, sondern auch den Text, den
du dazu geschrieben hast. :)

ruppert@hs-worms.de schrieb am 22.06.20 um 12:36:
> das unten befindliche Programm funktioniert mit f-String-Formatierung
> nicht so, wie ich das erwarte. Wenn ich das F-String-Format, das ich
> haben möchte, zuerst als Striing formatiere (s), sieht es richtig aus.

Du meinst wohl diese Zeilen:

    f = "{{:0{:1d}d}} {{:0{:1d}b}} {{:0{:1d}b}}".format(dl,fw,fw)

    # s wird in der for-Schleife unten nicht interpretiert,
    # sondern als string ausgegeben:

    s = 'f"' + str(f"{{i:0{dl}d}} {{gray:0{fw}b}} {{bin:0{fw}b}}") + '"'

bzw. wenn ich mir nur den f-string ansehe:

    >>> dl = 5
    >>> fw = 6
    >>> f"{{i:0{dl}d}} {{gray:0{fw}b}} {{bin:0{fw}b}}"
    '{i:05d} {gray:06b} {bin:06b}'

Für mich sieht das soweit erstmal korrekt aus. Ich vermute, du möchtest
etwas machen, was mit .format() funktioniert aber mit f-strings nicht: den
Formatstring programmatisch erzeugen statt hinzuschreiben, stimmt das?
f-strings sind ein festes Sprachkonstrukt, das direkt in der Sprache (bzw.
im Parser) auf String-Literalen ausgewertet wird, daher ist es nicht
möglich, f-strings zu generieren. (Ok, du könntest eval() verwenden, aber
das ist recht langsam und auch fehleranfällig – tu's lieber nicht.)
".format()" dagegen wird dynamisch auf beliebigen Strings ausgewertet,
nicht nur auf Literalen.

Du kannst .format() und f-strings aber auch mischen:

    formatiere_ausgabe = \
        f"{{i:0{dl}d}} {{gray:0{fw}b}} {{bin:0{fw}b}}".format

    print(formatiere_ausgabe(i=i, gray=gray, bin=bin))

Hier ist "formatiere_ausgabe" die ".format" String-Methode, angewendet auf
meinen Formatierungsstring, den ich per f-string erzeugt habe. Wenn ich
diese Methode dann aufrufe, und meine drei Argumente übergebe, dann wird
meine Ausgabe entsprechend dem Formatierungsstring zusammengestellt.

Stefan

Back to de.comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

f-string Formatanweisung ruppert@hs-worms.de - 2020-06-22 10:36 +0000
  Re: [Python-de] f-string Formatanweisung Mike Müller <mmueller@python-academy.de> - 2020-06-22 14:27 +0200
  Re: [Python-de] f-string Formatanweisung Mike Müller <mmueller@python-academy.de> - 2020-06-22 14:31 +0200
    Re: [Python-de] f-string Formatanweisung ruppert@hs-worms.de - 2020-06-22 13:22 +0000
  Re: [Python-de] f-string Formatanweisung Stefan Behnel <python-de@behnel.de> - 2020-06-22 14:55 +0200
  Re: f-string Formatanweisung "Andreas B." <ab@sysing.de> - 2020-06-22 15:03 +0200
    Re: f-string Formatanweisung ruppert@hs-worms.de - 2020-06-22 13:28 +0000
  Re: [Python-de] f-string Formatanweisung Stefan Behnel <python-de@behnel.de> - 2020-06-22 15:14 +0200
    Re: [Python-de] f-string Formatanweisung ruppert@hs-worms.de - 2020-06-22 14:03 +0000
    Re: [Python-de] f-string Formatanweisung ruppert@hs-worms.de - 2020-07-13 11:13 +0000

csiph-web