Python Forum
Re writing poste to make sense please help me
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Re writing poste to make sense please help me
#8
(Apr-01-2018, 11:15 AM)Nearrivers Wrote: Also is f-string like print
Not like print which is a function,f-string is string formatting with super power Wink
The history of string formatting.
city = 'Oslo'
description = 'Windy'
temperature = 25

# Old way don't use it
print('%s is currently %s and temperature is %dC' % (city, description, temperature))

# Python 2.6(10-year ago) we get .format()
print('{0} is currently {1} and temperature is {2}C'.format(city, description, temperature))

# Python 3.6 we get f-string
print(f'{city} is currently {description} and temperature is {temperature}C')
All output the same:
Output:
Oslo is currently Windy and temperature is 25C
Some super power.
>>> # f-strings support any Python expressions inside the curly braces
>>> name = 'f-string'
>>> print(f"My cool string formatting is called {name.upper():*^20}")
My cool string formatting is called ******F-STRING******

>>> 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


Messages In This Thread
RE: Re writing poste to make sense please help me - by snippsat - Apr-01-2018, 12:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Can anyone make sense of this? steve_shambles 7 3,328 Apr-19-2020, 11:22 AM
Last Post: steve_shambles
  These boolean statements don't make sense? Athenaeum 6 5,145 Oct-03-2017, 03:34 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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