Tutorial D1

11 important questions on Tutorial D1

Convertion van string-int en int-string(2)

Door formatting en regular expressions

Integer format (%d)


substitute a number and optional sign, without fractional part – float numbers are truncated


'>%d<  -> 65.2|65.5|65.7|65.99999999999999999

>65<

Exponential format (%E)


substitute a number in “scientific” notation, i.e. with an “E” and exponent, with optional sign; use “%e” to for lower case exponent letter


'>%E<'
print formats1 % (65)
>6.500000E+01<

print formats1 % (65.799999999) 
>6.580000E+01<

print formats1 % (0.0005)
>5.000000E-04<

print formats1 % (650)
>6.500000E+02<
  • Higher grades + faster learning
  • Never study anything twice
  • 100% sure, 100% understanding
Discover Study Smart

String format (%s):

substitute a string representation; applies str to the data value before substitution

formats1 = '>%s<'
print formats1 % ('name')

>name<

print formats1 %(65)
>65<

formats1 = '%s'
print formats1 % ('name')

name

Character format (%c):


substitute a character; if the value is an int, applies chr first




print '%c' %-85756848.8585
Traceback (most recent call last):

  File "<pyshell#26>", line 1, in <module>
    print '%c' %-85756848.8585-> TypeError: integer argument expected, got float
-> OverflowError: unsigned byte integer is less than minimum   #print '%c' %-85756848
-> OverflowError: unsigned byte integer is greater than maximum##print '%c' %85756848   

formats1 = '%c'
print formats1 % (85)
U

formats1 = '%c'

print formats1 % ('u')
u

Print formats1 % (65, 0.0001, 0.0001, 'name', 'X')


formats1 = '>%d< >%f< >%E< >%s< >%c< >%%<'

>65< >0.000100< >1.000000E-04< >name< >X< >%<

Belangrijk doel van string formatting:

align data in tidy(nette/ordelijk) columns in output – i.e. either printed output or an output file

String formatting: punt

Onderdeel van veld breedte (field width)

String formatting: ster

Variabele veld breedte (variable field width)

String formatting: min teken

Opstelling(alignment/layout)

Separate field width for the fractional part.

“%6.3f” (Fractional part.second field) = 6 cijfers in totaal waarvan er 3 achter de komma zijn.
Format6_3f = ‘>%6.3f<’
Print format6_3f % (65)
>65.000<

Of

Format6_3E = ‘>%63E<’
Print format6_3E % (65)
>6.50000E+01<

The question on the page originate from the summary of the following study material:

  • A unique study and practice tool
  • Never study anything twice again
  • Get the grades you hope for
  • 100% sure, 100% understanding
Remember faster, study better. Scientifically proven.
Trustpilot Logo