Python Forum
Counting words in the last line of a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Counting words in the last line of a file
#16
(May-31-2019, 10:18 AM)Drone4four Wrote: Would someone kindly explain, {num:>{max_num}}?
It's string formatting,< ^ > aligned left middle and right.
>>> left = '<left aligned>'
>>> middle = '<stay in middle>'
>>> print(f'aaa {left:>20} bbb {middle:^30} ccc')
aaa       <left aligned> bbb        <stay in middle>        ccc


>>> print(f"Sammy has {4:4} red and {16:16}! blue balloons")
Sammy has    4 red and               16! blue balloons

 
>>> for word in 'f-strings are awesome'.split():
...     print(f'{word.upper():~^20}')
...     
~~~~~F-STRINGS~~~~~~
~~~~~~~~ARE~~~~~~~~~
~~~~~~AWESOME~~~~~~~
 
>>> for word in 'f-strings are awesome'.split():
...     print(f'{word.upper():~<20}')
...     
F-STRINGS~~~~~~~~~~~
ARE~~~~~~~~~~~~~~~~~
AWESOME~~~~~~~~~~~~~

>>> for word in 'f-strings are awesome'.split():
...     print(f'{word.upper():~>20}')
...     
~~~~~~~~~~~F-STRINGS
~~~~~~~~~~~~~~~~~ARE
~~~~~~~~~~~~~AWESOME
Reply


Messages In This Thread
RE: Counting words in the last line of a file - by snippsat - May-31-2019, 09:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,680 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Getting last line of each line occurrence in a file tester_V 1 893 Jan-31-2023, 09:29 PM
Last Post: deanhystad
  Need to match two words in a line tester_V 2 897 Nov-18-2022, 03:13 AM
Last Post: tester_V
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,415 Sep-27-2022, 01:38 PM
Last Post: buran
  Print to a New Line when Appending File DaveG 0 1,240 Mar-30-2022, 04:14 AM
Last Post: DaveG
  Find and delete above a certain line in text file cubangt 12 3,561 Mar-18-2022, 07:49 PM
Last Post: snippsat
  CSV to Text File and write a line in newline atomxkai 4 2,754 Feb-15-2022, 08:06 PM
Last Post: atomxkai
  writelines only writes one line to file gr3yali3n 2 2,427 Dec-05-2021, 10:02 PM
Last Post: gr3yali3n
  Extract a string between 2 words from a text file OscarBoots 2 1,899 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,871 Aug-11-2021, 03:45 PM
Last Post: jamesaarr

Forum Jump:

User Panel Messages

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