-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path28_String Format.py
More file actions
48 lines (37 loc) · 1.8 KB
/
28_String Format.py
File metadata and controls
48 lines (37 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# str.format() = optional method that gives users more
# control when displaying output
# Placeholder
animal = "cow"
item = "moon"
#print("the "+animal+" jumped over the "+item)
#print("The {} jumped over the {}".format("cow","moon"))
#print("The {} jumped over the {}".format(animal,item)) # {} known as placeholder
#print("The {} jumped over the {}".format(item,animal)) # inserting values to placeholder is known as positional argument
#print("The {0} jumped over the {1}".format(animal,item))
#print("The {1} jumped over the {0}".format(animal,item))
#print("The {animal} jumped over the {item}".format(animal="cow",item="moon")) # keyword argument
#print("The {item} jumped over the {animal}".format(animal="cow",item="moon"))
#print("The {animal} jumped over the {animal}".format(animal="cow",item="moon"))
#print("The {1} jumped over the {1}".format(animal,item))
#text = "The {} jumped over the {}"
#print(text.format(animal,item))
# Padding
# name = "Mandar"
#
# print("Hello, my name is {}".format(name))
# print("Hello, my name is {:10}".format(name)) # makes more spaces in o/p
# print("Hello, my name is {:10}. Nice to meet you".format(name))
# print("Hello, my name is {:<10}. Nice to meet you".format(name))
# print("Hello, my name is {:>10}. Nice to meet you".format(name))
# print("Hello, my name is {:^10}. Nice to meet you".format(name))
# formatting numbers
# number = 3.14159
#
# #print("The number pi is {:.2f}".format(number)) # f is floating point number
# print("The number pi is {:.3f}".format(number))
number = 1000
#print("The number is {:,}".format(number))
print("The number is {:b}".format(number)) # binary
print("The number is {:o}".format(number)) # octal
print("The number is {:X}".format(number)) # hexadecimal
print("The number is {:E}".format(number)) # scientific notation