-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_exercise.py
More file actions
125 lines (96 loc) · 2.65 KB
/
string_exercise.py
File metadata and controls
125 lines (96 loc) · 2.65 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Exercise 1: String Literals
# Double qutations marks inside the string
a = 'hi, "/hello world/"'
print(a)
#An apostrophe inside the string
b = "hi, how's it goint?"
print(b)
#Multiple lines with preserrving white space
c = """ this is a string,
with multiple lines and preserving white space"""
print(c)
#coded on multiple lines but printed on a single line
d = """ this is a string, \
with multiple lines \
but printed on a single line"""
print(d)
#Exercise 2: String Concatenation
# create a two string and concenate them also # add a space between them
first_name = "Chirag"
middle_name = "Baldevbhai"
last_name = "Patel"
print(first_name + " "+ middle_name + " " + last_name)
a = "bazinga"
print(a[2:6])
print(a[2:6:1]) # slicing with step
print(a[-5:-1])
# Exercise 3: String upper and lower case
a = "Animals"
b = "Badger"
c = "Honey Bee"
d = "Honey Badger"
print(a.lower())
print(b.lower())
print(c.lower())
print(d.lower())
print(a.upper())
print(b.upper())
print(c.upper())
print(d.upper())
#Exercise 4: String Space removal
a = " Filet Mignon"
b = "Brisket "
c = " Cheeseburger "
print(a.lstrip())
print(b.rstrip())
print(c.strip())
#Exercise 5: String startswith
a = "Becomes"
b = "becomes"
c = "BEAR"
d = " bEautiful"
print(a.startswith("be"))
print(b.startswith("be"))
print(c.startswith("be"))
print(d.startswith("be"))
print(a.lower().startswith("be"))
print(b.startswith("be"))
print(c.lower().startswith("be"))
print(d.lower().lstrip().startswith("be"))
#Exercise 6: create a user input
# 1 create a user input
user_input = input("Enter a string:")
print(user_input.lower())
print(len(user_input))
#Exercise 7: Pick Apart your user input
a = input("Tell me your password:")
print("The first letter you entered was:",a[0].upper())
#Exercise 8: working with strings and numbers
a = int(input("Enter a first number: "))
b = int(input("Enter a second number: "))
c = float(a * b)
print(f"The product of {a} and {b} is {c}.")
#Exercise 9: Streamline your point
weight = 0.2
animal = "newt"
# using format method
print("{weight} kg is the weight of the {animal}.".format(weight=weight, animal=animal))
#using f-string
print(f"{weight} kg is the weight of the {animal}.")
# exersie 10: find a substring
print("AAA".find("a"))
print("AAA".find("A"))
print("BAA".find("A"))
print("AAAS".find("AS"))
#Exercise 11: String replace
a = "Somebody said something to Samantha"
print(a.replace("S","X"))
# first replace all 'e' with '3' and then replace all 'a' with '4'
a = input("Enter a substring: ")
a = a.replace("e", "3")
a = a.replace("a", "4")
print(a)
# Second maethod used
a = a.replace("e", "3")\
.replace("a", "4")
print(a)