Skip to content

Commit e193831

Browse files
Add files via upload
1 parent 4ba80f9 commit e193831

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
#Strings Fruit Examples:
2+
3+
#1.Basic string Example:
4+
'''s1 = "apple"
5+
s2 = "banana"
6+
print(s1)
7+
print(s2)
8+
print(s1 + s2)
9+
print(3*s1)
10+
print(s2 * 3)
11+
print(2*s1+2*s2)'''
12+
13+
#Also use format() function to format strings. It also explained how you can
14+
#get the length of a string using the len() function.
15+
'''s1 = "orange"
16+
s2 = "banana"
17+
for letter in s2:
18+
if letter in s2:
19+
print(s1, "and", s2, "share the letter", letter)'''
20+
21+
#2. Multi-line Strings:
22+
23+
"""long = "I'm fed up with being treated like sheep. What's the \ point of going abroad if you're just another tourist carted\around in buses surrounded by sweaty mindless oafs from\
24+
Kettering and Conventry in their Cloth caps and their cardigans\and their transistor radios and their Sunday"
25+
print(long)"""
26+
27+
#3. Escape sequences.
28+
#"\n" is a so-called "escape sequence."
29+
#An escape sequence is a string character written as a backslash followed by
30+
#code, which can be one or multiple characters.
31+
32+
#4. String indices
33+
"""fruit = "orange"
34+
print(fruit[1])
35+
print(fruit[2])
36+
print(fruit[3])
37+
print(fruit[-2])
38+
print(fruit[-6])
39+
print(fruit[0])
40+
print(fruit[-3])"""
41+
42+
#4.1 You can also use variables as indices, and even calculations or function calls:
43+
44+
"""from math import sqrt
45+
46+
fruit = "orange"
47+
x = 3
48+
49+
print(fruit[3-2])
50+
print(fruit[int(sqrt(4))])
51+
print(fruit[2**2])
52+
print(fruit[int((x-len(fruit))/3)])"""
53+
54+
#4.2 Outside the bounds of the string:
55+
56+
"""fruit = "orange"
57+
print(fruit[:])
58+
print(fruit[0:])
59+
print(fruit[:6])
60+
print(fruit[:100])
61+
print(fruit[:len(fruit)])
62+
print(fruit[1:-1])
63+
print(fruit[2], fruit[1:6])"""
64+
65+
#4.3 Traversing Strings
66+
67+
#I already explained how you can traverse the characters of a string using a for loop:
68+
69+
'''fruit = 'apple'
70+
for char in fruit:
71+
print(char, '-', end = '')'''
72+
73+
#4.3.1 use indices Traverse string
74+
75+
'''fruit = 'apple'
76+
77+
for i in range(0, len(fruit)):
78+
print(fruit[1], "-", end="")
79+
print()
80+
81+
i = 0
82+
while i< len(fruit):
83+
print(fruit[i], "-", end="")
84+
i+=1'''
85+
86+
#4.4 Extended slices
87+
88+
"""fruit = "banana"
89+
print(fruit[::2])
90+
print(fruit[1::2])
91+
print(fruit[::-1])
92+
print(fruit[::-2])"""
93+
94+
#Reversing a string :
95+
96+
'''fruit = "banana"
97+
print(fruit[::-1])
98+
for i in range(5, -1, -1):
99+
print(fruit[i])'''
100+
101+
#5 Strings are immutable
102+
103+
'''fruit = "orange"
104+
fruit[2] = "a" #Runtime error!
105+
print(fruit)'''
106+
107+
#for instance:
108+
109+
'''fruit = "orange"
110+
fruit = fruit[:2] + "a" + fruit[3:]
111+
print(fruit)'''
112+
113+
#6 string methods
114+
#6.1 strip()
115+
'''s = " I have many Fruits \n "
116+
print("["+s+"]")
117+
s = s.strip()
118+
print("["+s+"]")'''
119+
120+
#6.2 upper() and lower()
121+
'''s = "The Meaning of Life"
122+
print(s)
123+
print(s.upper())
124+
print(s.lower())'''
125+
126+
#6.3 find()
127+
'''s = "We can decorate our garden by growing fruits "
128+
print(s.find("can"))
129+
print(s.find("t"))
130+
print(s.find("t", 12))
131+
print(s.find("g"))'''
132+
133+
#6.4 replace()
134+
'''a = 'We can decorate our garden by growing fruits'
135+
print(s.replace('fruits', 'Trees'))'''
136+
137+
#6.5 split()
138+
"""s = 'We can decorate our garden by growing fruits'
139+
wordlist = s.split()
140+
for word in wordlist:
141+
print(word)"""
142+
143+
#6.6 join()
144+
#join() is the opposite of split():
145+
'''s = 'We can; decorate our; garden by ; growing fruits'
146+
wordlist = s.split( ';' )
147+
s = "".join(wordlist)
148+
print(s)'''
149+
150+
151+
152+
153+
154+
155+
156+
157+
158+
159+
160+
161+
162+
163+
164+
165+
166+
167+
168+
169+
170+
171+
172+
173+
174+
175+
176+
177+
178+
179+
180+
181+
182+
183+
184+
185+
186+
187+

0 commit comments

Comments
 (0)