-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathformatting_Strings_in_python.py
70 lines (39 loc) · 1 KB
/
formatting_Strings_in_python.py
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
49
# s = "Hello"
# w = "World"
# # and using concatenation we print hello world by using
# print(s+w)
"""
String formatting
In python we can do string formatting in multiple ways
"""
# ------------------------------
"""
Way no. 1-> using % operator
%s, %d, %f these are called format specifier
%s -> string
%d -> integer
%f -> float
%b -> binary
%r -> boolean
"""
name = "Sanket"
year = 2021
happiness = 100.0
greeting = "Hello, %s to %d and we hope your happiness matric is %f" % (name, year, happiness)
print(greeting)
"""
Way no. 2-> using format function
"""
greeting = "Hello, {x}".format(x = name)
print(greeting)
greeting = "Hello, {x} to {y}".format( y = year, x = name)
print(greeting)
greeting = "Hello, {} to {} and we hope your happiness matric is {}".format(name, year, happiness)
print(greeting)
print(format(12, "b"))
"""
Way no. 3 -> F-strings
"""
binary = 0b101
greeting = f"Hello, {name} {~binary:0b} to {year} and we hope your happiness matric is {happiness}"
print(greeting)