Skip to content

Commit 68ced35

Browse files
authored
Add files via upload
Solutions by me
1 parent b3508ad commit 68ced35

1 file changed

Lines changed: 258 additions & 0 deletions

File tree

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": []
7+
},
8+
"kernelspec": {
9+
"name": "python3",
10+
"display_name": "Python 3"
11+
},
12+
"language_info": {
13+
"name": "python"
14+
}
15+
},
16+
"cells": [
17+
{
18+
"cell_type": "markdown",
19+
"source": [
20+
"# **Exercise: String in Python**"
21+
],
22+
"metadata": {
23+
"id": "wWdZb5sDGkVU"
24+
}
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"source": [
29+
"1.Create 3 variables to store street, city and country, now create address variable to store entire address. Use two ways of creating this variable, one using + operator and the other using f-string. Now Print the address in such a way that the street, city and country prints in a separate line."
30+
],
31+
"metadata": {
32+
"id": "HyF752biHTy4"
33+
}
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 6,
38+
"metadata": {
39+
"colab": {
40+
"base_uri": "https://localhost:8080/"
41+
},
42+
"id": "pmUMronzEofB",
43+
"outputId": "7a58ae98-cf82-475a-ef9e-dfd8f01f2aa0"
44+
},
45+
"outputs": [
46+
{
47+
"output_type": "stream",
48+
"name": "stdout",
49+
"text": [
50+
"using + operation: 24\n",
51+
"Nagpur\n",
52+
"india\n",
53+
"My full address is:24,city i lived in is:Nagpur,and im from:india\n"
54+
]
55+
}
56+
],
57+
"source": [
58+
"street = 24\n",
59+
"city =\"Nagpur\"\n",
60+
"country=\"india\"\n",
61+
"address = str(street) + \"\\n\" + city + \"\\n\" + country\n",
62+
"print(\"using + operation:\" ,address)\n",
63+
"\n",
64+
"print(f\"My full address is:{street},city i lived in is:{city},and im from:{country}\")"
65+
]
66+
},
67+
{
68+
"cell_type": "markdown",
69+
"source": [
70+
"**Explanation**:**Using + operator:**\n",
71+
"\n",
72+
"The + operator joins strings.\n",
73+
"\n",
74+
"\"\\n\" adds a new line, so each part of the address prints on a separate line.\n",
75+
"\n",
76+
"# **Using f-string:**\n",
77+
"\n",
78+
"f-strings allow you to embed variables directly inside a string using {}.\n",
79+
"\n",
80+
"f\"{street}\\n{city}\\n{country}\" does the same as above, but it's cleaner and easier to read."
81+
],
82+
"metadata": {
83+
"id": "JRYTIFyrKuUx"
84+
}
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"source": [
89+
"2.Create a variable to store the string \"Earth revolves around the sun\"\n",
90+
"\n",
91+
"Print \"revolves\" using slice operator\n",
92+
"\n",
93+
"Print \"sun\" using negative index"
94+
],
95+
"metadata": {
96+
"id": "CAxSd-1wLQ3k"
97+
}
98+
},
99+
{
100+
"cell_type": "code",
101+
"source": [
102+
"sentence = \"Earth revolves around the sun\"\n",
103+
"\n",
104+
"# Print \"revolves\" using slice operator\n",
105+
"print(sentence[6:14])\n",
106+
"\n",
107+
"# Print \"sun\" using negative indexing\n",
108+
"print(sentence[-3:])"
109+
],
110+
"metadata": {
111+
"colab": {
112+
"base_uri": "https://localhost:8080/"
113+
},
114+
"id": "U3M36AP6Lzml",
115+
"outputId": "fb213e93-4997-4957-cd25-1380c3166f7c"
116+
},
117+
"execution_count": 8,
118+
"outputs": [
119+
{
120+
"output_type": "stream",
121+
"name": "stdout",
122+
"text": [
123+
"revolves\n",
124+
"sun\n"
125+
]
126+
}
127+
]
128+
},
129+
{
130+
"cell_type": "markdown",
131+
"source": [
132+
"**Explanation:**\n",
133+
"sentence[6:14]\n",
134+
"\n",
135+
"This slices the string from index 6 up to (but not including) index 14.\n",
136+
"\n",
137+
"It gives: \"revolves\"\n",
138+
"\n",
139+
"sentence[-3:]\n",
140+
"\n",
141+
"Negative indexing starts from the end of the string.\n",
142+
"\n",
143+
"-3: means from the third character from the end to the end.\n",
144+
"\n",
145+
"It gives: \"sun\""
146+
],
147+
"metadata": {
148+
"id": "I1iZNgzmNZMY"
149+
}
150+
},
151+
{
152+
"cell_type": "markdown",
153+
"source": [
154+
"3.Create two variables to store how many fruits and vegetables you eat in a day. Now Print \"I eat x veggies and y fruits daily\" where x and y presents vegetables and fruits that you eat everyday. Use python f string for this."
155+
],
156+
"metadata": {
157+
"id": "EQ2R68e2NjH8"
158+
}
159+
},
160+
{
161+
"cell_type": "code",
162+
"source": [
163+
"fruits = 2\n",
164+
"vegetables = 3\n",
165+
"\n",
166+
"print(f\"I eat {vegetables} veggies and {fruits} fruits daily\")"
167+
],
168+
"metadata": {
169+
"colab": {
170+
"base_uri": "https://localhost:8080/"
171+
},
172+
"id": "F-UalNVdNpWm",
173+
"outputId": "c5e5970d-7a01-40cd-b5d8-119835122da1"
174+
},
175+
"execution_count": 9,
176+
"outputs": [
177+
{
178+
"output_type": "stream",
179+
"name": "stdout",
180+
"text": [
181+
"I eat 3 veggies and 2 fruits daily\n"
182+
]
183+
}
184+
]
185+
},
186+
{
187+
"cell_type": "markdown",
188+
"source": [
189+
"**Explanation:**\n",
190+
"\n",
191+
"fruits = 2\n",
192+
"\n",
193+
"stores how many fruits you eat daily.\n",
194+
"\n",
195+
"vegetables = 3\n",
196+
"\n",
197+
"stores how many vegetables you eat daily.\n",
198+
"\n",
199+
"The f-string lets you insert variables directly into the string using {}.\n",
200+
"\n"
201+
],
202+
"metadata": {
203+
"id": "MEprQw-MN4OH"
204+
}
205+
},
206+
{
207+
"cell_type": "markdown",
208+
"source": [
209+
"4.I have a string variable called s='maine 200 banana khaye'. This of course is a wrong statement, the correct statement is 'maine 10 samosa khaye'. Replace incorrect words in original strong with new ones and print the new string. Also try to do this in one line."
210+
],
211+
"metadata": {
212+
"id": "eb-neHDbOEGu"
213+
}
214+
},
215+
{
216+
"cell_type": "code",
217+
"source": [
218+
"s = 'maine 200 banana khaye'\n",
219+
"s = s.replace(\"200\" , \"10\").replace('banana' , 'samosa')\n",
220+
"print(s)\n"
221+
],
222+
"metadata": {
223+
"colab": {
224+
"base_uri": "https://localhost:8080/"
225+
},
226+
"id": "G29vvU0HOKrh",
227+
"outputId": "93f9e52b-5848-40ab-b97a-7b573a5e3b35"
228+
},
229+
"execution_count": 11,
230+
"outputs": [
231+
{
232+
"output_type": "stream",
233+
"name": "stdout",
234+
"text": [
235+
"maine 10 samosa khaye\n"
236+
]
237+
}
238+
]
239+
},
240+
{
241+
"cell_type": "markdown",
242+
"source": [
243+
"**Explanation:**\n",
244+
"\n",
245+
"s.replace(\"200\", \"10\") replaces \"200\" with \"10\".\n",
246+
"\n",
247+
"Chaining .replace() lets you do multiple replacements in one line.\n",
248+
"\n",
249+
"Final output:\n",
250+
"\n",
251+
" maine 10 samosa khaye\n"
252+
],
253+
"metadata": {
254+
"id": "WfDzE-yOPAO-"
255+
}
256+
}
257+
]
258+
}

0 commit comments

Comments
 (0)