Re: [Tutor] Python = {0}.format
abid saied abidsa...@gmail.com writes: I’m in the process of teaching myself python. Congratulations, and welcome. Can someone have a look at the text in red and explain please. Text comes through as text. Don't rely on fonts, colours, or other non-text markup to survive. If you want to draw attention to some part of code, it is much better to reduce the example so it is minimal enough to show only what is relevant, while still exhibiting the behaviour you want to discuss. print(The original quote is: {0}.format(quote)) print(The new quote is:{0}.format(quote.replace(replaceWord,replaceWith))) # Not sure what the {0}.format is doing Every value in Python is an object, with methods according to its type. A text string has many methods URL:https://docs.python.org/3/library/stdtypes.html#string-methods. The ‘format’ method of the ‘str’ type is used to generate formatted output from a template string. See URL:https://docs.python.org/3/library/stdtypes.html#str.format and URL:https://docs.python.org/3/library/string.html#formatstrings. -- \ “I have yet to see any problem, however complicated, which, | `\ when you looked at it in the right way, did not become still | _o__)more complicated.” —Paul Anderson | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python = {0}.format
On 17/08/14 15:50, abid saied wrote: Hi, I’m in the process of teaching myself python. Can someone have a look at the text in red and explain please. print(This program displays a given quote in different formats) print() # Why is this needed? Its not it only adds a bnlank line. You could achieve the same result by adding a \n character at the end of the last line. quote = input(Please enter a quote to format: ) print(quote) replaceWord = input(Which word in the quote would you like to replace: ) replaceWith = input(Please enter the word to replace with: ) print(The original quote is: {0}.format(quote)) # Not sure what the {0}.format is doing String formatting inserts data values into a string. Notice that the .format() occurs after the striong, not after the {} marker. The {} marks the place where the data will be inserted. The 0 indicates that its the zero'th (or first) item in the list that should be inserted. The list of data items is the list of parameters to format(). Here is an example: print ({0} x {1} = {2}.format(3,4,12) And another showing the value of the indexing: s = {0} can be written as {0} or {1}.format('four',4) print(s) You can have various other codes inside the {} to control spacing and justification or padding etc. Here is the official documentation page on the subject: https://docs.python.org/3/library/string.html#string-formatting HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python = {0}.format
print(The original quote is: {0}.format(quote)) print(The new quote is:{0}.format(quote.replace(replaceWord,replaceWith))) Hi Abid, You should be able to change this to the equivalent code: print(The original quote is: + quote) print(The new quote is: + quote.replace(replaceWord,replaceWith)) which does not use string formatting, but just plain string concatenation. I believe it should have the same meaning, since we know the things we're concatenating are strings. Python includes a mini templating language for having a strings with fill-me-ins, and letting you fill in the fill-me-ins later. In such a small example as the one above, I'd argue that templates are overkill. You'll note that the non-templated version is even shorter than the original code that uses templates. String templating makes a lot more sense outside of a toy context, when the output isn't a one-liner. If you have access to a good public library, find the classic book Jon Bentley's Programming Pearls and read Chapter 3 on Data Structures Programs: it discusses a rationale for Form Letter Programming. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor