Python Forum

Full Version: Newb question about %02d %04d
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
(Mar-05-2019, 09:44 PM)bennylava Wrote: [ -> ]Particularly the part where they had me do str(row_count). What is the purpose of that?
It convert integer to string,it's used because the string is bad build up.
We use string formatting to avoid + str() and many " in one string.
In 3.6 did we get f-string,that takes over for format().
It looks a lot better like this.
def create_spreadsheet(title, row_count=1000):
      print(f"Creating a spreadsheet called {title} with {row_count} rows")

create_spreadsheet(title = "Applications", row_count=10)
Output:
Creating a spreadsheet called Applications with 10 rows
(Mar-05-2019, 09:44 PM)bennylava Wrote: [ -> ]Can someone go into when you can use commas, and when you can't?
Can look at is a separator between values/elements that used is many different cases.
In function over it separate function parameter title and row_count=1000
With two values it will separate automatic into a tuple.
>>> 1, 2
(1, 2)
>>> 'hello', 'world', 99
('hello', 'world', 99)
In a list a natural way to separate elements.
>>> lst = ['hello', 'car', 999]
>>> lst
['hello', 'car', 999]
>>> lst[2]
999
Pages: 1 2 3 4