+ 3

question

who can that explain? I dont understand the meaning of „%s in the code txt = "snow world" print("%s" % txt[4:7]) many thanks https://code.sololearn.com/cY1Qqcd6awir/?ref=app

28th Jun 2023, 9:41 AM
Angela
Angela - avatar
5 Answers
+ 5
is this *old- fashioned code* really a question in a current challenge?
28th Jun 2023, 1:20 PM
Lothar
Lothar - avatar
+ 4
It is the old fashion way of string formatting. %s is a place holder and it is a string. So the print statement says: print a string (referring to %s), and the content (%) is txt[4:7]. Similarly there is a %d which means a number; %f means float, and other which I really can’t remember.
28th Jun 2023, 9:51 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 3
yes it was a question from today
28th Jun 2023, 1:32 PM
Angela
Angela - avatar
+ 2
I found some challenges introduce functions which are not covered in the courses, or at least I haven't seen them in the courses. For example: iter(). Maybe they were discussed in the older courses which are "retired"? Usually I go over the questions after challenge, and look for what I was wrong or don't understand, and then fire up Python IDLE to make test or search on web for more info.
28th Jun 2023, 2:16 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
Angela Google for "Python c-style print formatting". If you come from a c programming background, you might be familiar with it from printf(). The new way is to use f-strings. But if you are learning from older reference materials, understaning how to use this syntax is useful. here is a good site: https://www.learnpython.org/en/String_Formatting
28th Jun 2023, 2:57 PM
Bob_Li
Bob_Li - avatar