ASCII and Print Formatting
ASCII - american standard code for information interchange
ord("alphanumeric")==ascii code
chr(ascii code)==alphanumeric
A-Z -65-90
a-z -97-122
0-9 48-57
immutable -we can't change their values in place. in place imp.
Print Formatting
print(file=file_location)
print(f/F"text {special value}")
print(f'{variable_name = }') #output == variable_name = variable_value
print(f'{variable_name!a}')==show ascii code instead of symbol if any
print(f'{variable_name!r}')==print(repr(variable_name))
print(f'{variable_name!s}')== simple str print
str vs repr is that repr also print the inverted commas (')
repr()-it is generallly used for debugging
for custom class objects it returns the name and address of object inside <>.
Formatting:
f'{date_variable:%Y %m %d}'
f'{deci_val:.nf}'#n= number of decimal values to be printed
class :
class MyClass:
def __format__(self,format_spec) -> str:
print(f'Text in class method and {format_spec=!r}')
return "TEXT RETURNed by class"
print(f'{MyClass():Text in fstring}')
output:
Text in class method and format_spec='Text in fstring'
Writing r before a string opening “ treats all the \ as backslash and not as escape sequence
TEXT RETURN
%c – Returns a Single character.
%d – Returns a Decimal Integer
%i – for Long integer
%u – Returns an unsigned decimal integer
%e, %E – Returns the floating-point value in exponential notation.
%f – Returns the floating-point value in fixed-point notation.
%g – Returns the shorter value of %f and %e
%G – Returns the shorter value of %f and %E
%c – Returns a Single character
%o – Returns an Octal value
%r – Generates string with repr()
%s – Converts the value to a string using str() function.
%x, %X – Returns the Hexadecimal integer.
Comments
Post a Comment
For any queries or suggestions
Comment Here