Python Forum
week 1 python help - string formatting ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
week 1 python help - string formatting ?
#21
Now that looked at format() forget it,in my option learn and only use f-string.
f-string are far more readable and concise,less prone to error than other ways of formatting,they are also faster!
>>> first_name = 'Eric'
>>> last_name = 'Idle'
>>> print(f'Welcome {first_name} {last_name} to Python forum')
Welcome Eric Idle to Python forum

>>> name = 'f-string'
>>> print(f"String formatting is called {name.upper():*^20}")
String formatting is called ******F-STRING******
 
# f-strings can take any Python expressions inside the curly braces.
>>> cost = 99.75999
>>> finance = 50000
>>> print(f'Toltal cost {cost + finance:.2f}')
Toltal cost 50099.76
 
>>> for word in 'f-strings are cool'.split():
...     print(f'{word.upper():~^20}')
...
~~~~~F-STRINGS~~~~~~
~~~~~~~~ARE~~~~~~~~~
~~~~~~~~COOL~~~~~~~~
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  String formatting - tax brackets darek88 1 1,969 Aug-28-2020, 09:59 AM
Last Post: Larz60+
  Calculating the number of day of the week RbaPhoenix 3 2,433 Mar-27-2020, 09:23 PM
Last Post: Larz60+
  String formatting issues? lolatyou 2 2,038 Mar-08-2020, 06:53 AM
Last Post: michael1789
  hardest week yet: some guidence please raymond2688 25 10,541 Aug-22-2019, 05:23 PM
Last Post: ichabod801
  Trying to get the week number from a string of numbers fad3r 2 3,159 Apr-15-2018, 06:52 PM
Last Post: ljmetzger

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020