String formatting in python and Triple Quotes:
This feature is inherited from language C. Python uses the % symbol for String format.
Example:
# This program will "Hello, Ravi ji"
name = "Ravi ji"
print("Hello, %s" % name)
# This program will "Ravi is 23 years old now."
name = "Ravi"
age = 25
print("%s is %d years old now." % (name, age))
# This program will print: A list: [1, 2, 3]
mylist = [1,2,3]
print("A list: %s" % mylist)
List of String format with symbol and Description:
| Format Symbol/notation | Conversion meaning |
|---|---|
| %c | print character character |
| %s | string conversion (to string)via str() prior to formatting method |
| %i | print signed decimal integer |
| %d | print signed decimal integer |
| %u | print unsigned decimal integer |
| %o | print octal integer |
| %x | print hexadecimal integer (lowercase letters) |
| %X | print hexadecimal integer (UPPERcase letters) |
| %e | print exponential notation (with lowercase ‘e’) |
| %E | print exponential notation (with UPPERcase ‘E’) |
| %f | print floating point real number |
| %g | print the shorter of %f and %e |
| %G | print the shorter of %f and %E |
Triple Quotes
Triple Quotes role extremely imperative in python. Inside every character inside the quotes is treated as its own meaning. If you take n symbol then it will treat as a new line. It is the same as the pre tag in HTML.
Python’s triple quotes derive to the salvage by allowing strings to span various lines, including NEWLINE, TABs, and any more special characters written.
Here every single special character has been transformed into its printed form.
Example:
str12 = """Life is long, and person should honest
TAB ( t ) never treats others like animal [ n ]. By
"""
print (str12)
print (Happy codding)
