Diferències

Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.

Enllaç a la visualització de la comparació

Ambdós costats versió prèvia Revisió prèvia
Següent revisió
Revisió prèvia
development:python:strings [12/09/2022 00:30] – [srtring fill] matedevelopment:python:strings [10/10/2024 00:53] (actual) – [f-strings (Literal String Interpolation)] mate
Línia 1: Línia 1:
 = Python strings = Python strings
 +{{tag>python}}
 == comp == comp
   * Exact match (equality comparison): ==, !=   * Exact match (equality comparison): ==, !=
Línia 13: Línia 14:
     * ''"string%s"%(param1)''     * ''"string%s"%(param1)''
     * ''"string {} {}".format(1parm,2param)''     * ''"string {} {}".format(1parm,2param)''
-    * ''"string {1}{2}{1}".format(1param,2param"'' +    * ''"string {1}{2}{1}".format(1param,2param)'' 
-    * interpolación: ''f"string {name} {a + b}"''+    * interpolación (f-string): ''f"string {name} {a + b}"''
     * Template strings     * Template strings
  
-== srtring fill+== f-strings (Literal String Interpolation) 
 +  * To create an f-string, prefix the string with the letter “ f ”. The string itself can be formatted in much the same way that you would with str.format(). F-strings provide a concise and convenient way to embed python expressions inside string literals for formatting. 
 +  * F-strings are faster than the two most commonly used string formatting mechanisms, which are % formatting and str.format(). 
 +<code python> 
 +val = 'Geeks' 
 +print(f"{val}for{val} is a portal for {val}."
 +</code> 
 +<code python> 
 +import datetime 
 +  
 +today = datetime.datetime.today() 
 +print(f"{today:%B %d, %Y}"
 +</code> 
 +Backslash Cannot be used in format string directly. 
 +<code python error> 
 +f"newline: {ord('\n')}" 
 +</code> 
 +<code python correcto> 
 +newline = ord('\n'
 +  
 +print(f"newline: {newline}"
 +</code> 
 +/+info: [[https://www.python.org/dev/peps/pep-0498/]] 
 +/via: [[https://www.geeksforgeeks.org/formatted-string-literals-f-strings-python/]] 
 + 
 +== string fill
   * ''ljust()'', ''rjust()'', ''center()''   * ''ljust()'', ''rjust()'', ''center()''
   * ''%15s'', ''%15-s''   * ''%15s'', ''%15-s''
   * ''{:15}'', ''{:>15}''   * ''{:15}'', ''{:>15}''
 /via:[[https://www.delftstack.com/es/howto/python/python-pad-string-with-spaces/]] /via:[[https://www.delftstack.com/es/howto/python/python-pad-string-with-spaces/]]
 +  * ''zfill()''
 +
 +== zero fill
 +  * ''n=42''
 +  * ''print(f'{n:05d}')''
 +  * ''print('%05d' % n)''
 +  * ''print('{0:05d}'.format(n))''
 +  * 00042
 + /via: [[https://www.python-engineer.com/posts/pad-zeros-string/]]
 +== concatenate
 +  * ''s1+s2''
 +  * ''"%s %s"%(s1,s2)''
 +  * ''"-".join([s1,s2])''
 +  * ''"{}{}".format(s1,s2)''
 +
 +/via: [[https://www.tutorialspoint.com/how-to-concatenate-two-strings-in-python]]
  • development/python/strings.1662967832.txt.gz
  • Darrera modificació: 12/09/2022 00:30
  • per mate