Dream on Python

Posts About Python & Programming

Strings and String Formatting

Ways to Format Strings in Python (May 4)

The following statement prints text without additional processing:

print("In 2021 the GDP of America is $22.99T")

We get:

In 2021 the GDP of America is $22.99T

Suppose the parameters (country name, year, and GDP) are given in variables:

year = 2021
country = 'America'
value = 22.99

The following ways of forming text produce the same result:

print('In ' + str(year) + ' the GDP of ' + country + ' is $' + str(value) + 'T')
print('In %d the GDP of %s is $%.2fT' % (year, country, value))
print('In {} the GDP of {} is ${}T'.format(year, country, value))
print('In {0} the GDP of {1} is ${2}T'.format(year, country, value))
print('In {year} the GDP of {country} is ${value}T'.format(
    year=year, country=country, value=value
))
data = {'year':year, 'country':country, 'value':value}
print('In {year} the GDP of {country} is ${value}T'.format(**data))
print(f'In {year} the GDP of {country} is ${value}T')

The most modern is the last option, which uses f-string (fast string).

Code in OnlineGDB


Forming Single-line and Multi-line Text in Python (May 4)

The following four examples form single-line text:

print('"There should be no such thing as boring mathematics." -- Dijkstra\'s quote')
print("\"There should be no such thing as boring mathematics.\" -- Dijkstra's quote")
print('"There should be no such thing \
as boring mathematics." \
-- Dijkstra\'s quote')
print('"There should be no such thing '
      + 'as boring mathematics." '
      + "-- Dijkstra's quote")

Output to screen:

"There should be no such thing as boring mathematics." -- Dijkstra's quote

And the following five examples form multi-line (more precisely, two-line) text:

print('"There should be no such thing as boring mathematics." \n\t-- Dijkstra\'s quote')
print("\"There should be no such thing as boring mathematics.\" \n\t-- Dijkstra's quote")
quote = '"There should be no such thing as boring mathematics." \n\
\t-- Dijkstra\'s quote'
print(quote)
print(""""There should be no such thing as boring mathematics."
    -- Dijkstra\'s quote""")
quote = """"There should be no such thing as boring mathematics."
    -- Dijkstra\'s quote"""
print(quote)

Output to screen:

"There should be no such thing as boring mathematics."
    -- Dijkstra's quote

The code to play: OnlineGDB


Introduction to str.split() and str.join() Methods (May 4)

We use split() to divide text into words.

And to join words, we use the reverse method: join().

quote = 'Simplicity is prerequisite for reliability.'
print(f"Dijkstra's quote: {quote}")

words = quote.split(' ')
print(f"Split using ' ':  {words}")

text = ' '.join(words)
print(f"Join using ' ':   {text}")
print()

You can use method composition (call a method on the result of the previous method):

print(' '.join(quote.split()))
print(' '.join(words).split(' '))
print()

And now let’s compare that the result of the work of two functions produces the original:

print(f'the produced text equals original quote: {text == quote}')
print()

print(f'" ".join(quote.split(" ")) == quote: {" ".join(quote.split(" ")) == quote}')

print(f'" ".join(words).split(" ") == words: {" ".join(words).split(" ") == words}')

Code to play: OnlineGDB