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


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

Re: [Python-de] f-string Formatanweisung

From Mike Müller <mmueller@python-academy.de>
Newsgroups de.comp.lang.python
Subject Re: [Python-de] f-string Formatanweisung
Date 2020-06-22 14:27 +0200
Organization Python Academy GmbH & Co. KG
Message-ID <mailman.250.1592828877.24731.python-de@python.org> (permalink)
References <rcq1j2$5b5$1@dont-email.me> <0ce577a1-9f85-f36b-b20b-bd3dd739f511@python-academy.de>

Show all headers | View raw


Das `f` gehört vor den String:

a = 5

print(f'{a}')

nicht in den String:

print(f"'{a}'")

Viele Grüße
Mike

Am 22.06.20 um 12:36 schrieb ruppert@hs-worms.de:
> Hallo,
> 
> 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.
> 
> Aber bei print(s) wird nicht das in s steckende f-string-Format
> interpretiert, sondern einfach nur s als String ausgegeben.
> 
> Das Programm soll übrigens Binärzahlen in Gray umwandeln und zurück.
> Mit der .format-Methode funktioniert das auch einwandfrei.
> 
> Aufruf des Programms in der kürzesten Weise:
> 
> grayCode.py 2 1
> 
> 2 ist der kürzeste Graycode und 1 schaltet in den debug-Modus
> 
> Vielen Dank
> 
> Martin Ruppert
> 
> #!/usr/bin/python3
> import sys
> if(len(sys.argv)<2):
>     a=sys.argv[0].split('/')
>     a=a[len(a)-1]
>     print('usage:',a,'digits of GrayCode')
>     print('   eg.',a,'12')
>     exit(1)
> 
> def bintogray(bin):
>     return(bin^(bin>>1))
>     
> def graytobin(n,gray):
>     val=0
>     for i in range(n,-1,-1):
>         val = val|((((1<<(i+1))&val)>>1)^((1<<i)&gray))
>     return(val)
> 
> dbg=0
> if(len(sys.argv)>2):dbg=int(sys.argv[2])
> fl=int(sys.argv[1])
> fw=fl
> if(fw<4):fw=4
> dl=int(fl/3.321928+1) #dezimale Stellen
> if(dl<4):dl=4
> f="%%-%ds %%-%ds %%-%ds"%(dl,fw,fw)
> if(dbg>0):print("f=",f)
> h=f%("#val","gray","bin")
> 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}}")+'"'
> if(dbg>0):print("f=",f)
> if(dbg>0):print("s=",s)
> for i in range(2**fl):
>     if(i%24==0):print(h)
>     gray=bintogray(i)
>     bin=graytobin(fl,gray)
>     print(f.format(i,gray,bin))
>     s='f"'+(f"{{i:0{dl}d}} {{gray:0{fw}b}} {{bin:0{fw}b}}")+'"'
>     print(s)
> 

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