· 4 years ago · Aug 30, 2021, 04:20 PM
1
2print("My string Fun")
3
4food = "kartupelis"
5print(type(food))
6food = "auzu putra"
7food = "kartupelis"
8# # # # # # food
9# # # # # # # Python Slicing syntax
10print(food)
11print(food[0]) # why zero index, historic reasons
12
13food_len = len(food) # how many letters in a string or other collection
14
15print(food_len)
16print(food[9]) # so index 9 means 10th element
17print(food[len(food)-1]) # not pythonic, avoid! Not needed
18print(food[-1]) # this is Pythonic way of getting last character of string
19print(food[-2])
20print(food[2]) # what will be printed ?
21# print(food[555]) # IndexError: string index out of range
22# print(food[-11]) # IndexError: string index out of range
23
24# # # # # # # when slicing we do not include the last index so 5 would be 6th element which we do not include
25print(food[0:5], food[:5]) # so index 5(6th element) is not included so we get 1st 5 elements
26print(food[:9015]) # when slicing end index can be out of range
27print(food[-3000:9015]) # when slicing beginning and end index can be out of range
28print(food[-3000:]) # when slicing beginning and end index can be out of range
29# print(food[:]) #not much point for strings but for other sequences it makes a copy
30# print(food)
31
32print(food[5:10], food[5:], food[-5:])
33food = "Auzu putra ar avenēm un krejumu"
34print(food[5:10], food[5:], food[-5:], sep="\n")
35# # # # # print(food[400]) # so too much positive will give IndexError
36# # # # # print(food[-320]) # so too much negative will give IndexError
37# # # print(food[-320:400]) # we can do this though
38# # # print(food[-10:400]) # we can do this though
39
40# print(food[5:], food[1:])
41# print(food)
42print(food[0:20:2]) # step can be any whole number
43print(food[:20:2])
44print(food[::2]) # this would get 2nd character of any strings
45print(food[1::2]) # this would get 2nd character of any strings starting with 2nd char
46# # # # # # print(len(food))
47
48# # # # # # # reversing a string
49print(food[::-1]) # so we go backwards
50my_reverse_string = food[::-1] # if I need to save it
51print(my_reverse_string)
52
53words = food.split(" ")
54print(words)
55
56# # # # if i need ' inside string then I can use " outside
57my_text = "Alice told the rabbit 'go down the hole' and then followed"
58print(my_text)
59another_text = 'The bottle said "Eat me!" and Alice couldnt not write single quotes'
60print(another_text)
61multi_line_string = f"""We can write whatever even go to a
62new line
63 or use tab
64 use single ' and also "
65 and so o n
66 un man patīk {food}
67
68 4wt!@#$^&
69"""
70print(multi_line_string)
71escaped_string = "text meaning \" and also \' \n \t and of course \\ so on "
72print(escaped_string)
73# # # # # # food[::-2]
74print(f"Min ({min(food)}), max({max(food)}), len:{len(food)}") #min and max use unicode codes
75print(ord(" "), ord("e"), ord("ē")) # Return the Unicode code point for a one-character string
76
77# # # # # # this will not work
78# food[3] = "Z" # will not work
79# # # # # # # strings are immutable so we need to create new strings
80new_food = food.replace('u', 'ū') #replaces in all places by default count= can specify how many
81print(new_food)
82new_food = food.replace('u', 'ū', count:=2) #replaces in all places by default count= can specify how many
83print(new_food)
84# print("X"*len(new_food)) #i can multiple strings with numbers so 20 Xses
85
86print(food[:3])
87print(food[3]) # so we will not keep this one in the new string
88print(food[4:])
89ze_food = food[:3] + "ZXZ" + food[4:] # so build a new string
90print(ze_food)
91ze_food_2 = f"{food[:6]}*Jaunais*{food[4:]}"
92print(ze_food_2)
93food = "mazaiSSSS karTUPelis jaukais"
94# # cap_food = food.capitalize() # so first word in capital letters
95print(food.capitalize())
96print(food.title()) # Title Gives Uppercase To Each Word
97print(food.upper()) # YELLING
98print(food.lower())
99
100print(food.swapcase())
101
102print(food.count("S"))
103print(food.count("SS")) # answer should be 2 because we have SSS
104print(food.count("SSS")) # answer should be 2 because we have SSS
105print(food.count("ai"))
106# # # # # # print(food.capitalize())
107print(food.upper().replace("A", "Y")) # i can chain operations on strings
108# print(food[-5:].upper())
109# # print(food.count('a'))
110print(food.index('z')) # z is 3rd element so index is 2
111print(food.index('i')) # i is 5th element so index is 4
112# print(food[4])
113# # print(food.index("kart")) # so index throws ValueError if not found
114print(food.lower().index("kart"))
115# # print(food.find('z'))
116# # print(food.find('kart'))
117# # print(food.lower().find('kart'))
118# print(food[10:10+4])
119# # # # # # print(food.index('ž')) # index raises an error
120# # # print(food.find('ž')) # retursn -1
121# # # # # print(food.find('z'))
122# # # # # # print(ze_food)
123# # # # # # ze_food.count("Z")
124# # # # # # ze_food.find("p")
125# # # # # # ze_food.index("p")
126# # # # # # ze_food.find("valdis")
127# # # # # # ze_food.index("valdis")
128# # # # # # ze_food.find("ZZ")
129
130print("kar" in food) # so in gives us simple Boolean whether substring exists in string
131# print("kart" in food) # so in gives us simple Boolean whether substring exists in string
132# print("kart" in food.lower()) # so in gives us simple Boolean whether substring exists in string
133# print("karT" in food) # so in gives us simple Boolean whether substring exists in string
134is_found = "kar" in food
135print(is_found)
136
137for c in food:
138 print(c, end=":") # so instead of default newline \n i used : as endpoint
139
140for c in food[3:8]:
141 print(c)
142
143# # # # # # print(food.index('a'))
144count = 0
145char = "S"
146clean_text = ""
147for c in food:
148 if c == char:
149 count += 1
150 else:
151 clean_text += c # create new string by adding char to old string
152print(f"There are {count} {char}s in {food}")
153print(f"Cleaned {clean_text=}") # this syntax is starting from Python
154print(food.replace("S",""))
155
156# # # # # # food, clean_text
157
158fresh_text = ""
159for c1, c2 in zip(food, clean_text): # you can zip many sequences
160 if c1 == c2:
161 fresh_text += c1 # also c2 would work # not great for long texts
162 else: # mismatch
163 fresh_text += "_"
164print(fresh_text)
165
166# # # # isArtFound = "art" in food
167# # # # print(isArtFound)
168
169# # print("Valdis" < "Voldemars") # because 'a' , 'o' in Unicode table
170# # print(len("Valdis") < len("Voldemars")) # by length
171
172# # # # # # print("Alice said 'Run rabbit run!' and drunk something")
173# # # # # # print("When we need both quotes \" \\ and also ' \n\n new lines ")
174# # # # # # print(""" I can write whatever here
175# # # # # # even new lines
176# # # # # # with tabs
177# # # # # # ' " " " '
178# # # # # # """)
179# # # # # # print(food.upper())
180print(food.isalpha())
181# # # big_food = food + " banana"
182# # # print(big_food)
183# # # print(big_food.find("an"))
184# # # print(big_food.rfind("an"))
185city = " Rīga "
186print(city.strip())
187# # # print(big_food.find("a"))
188# # # print(big_food.rfind("a"))
189# # # # # # sentence = "A quick brown fox run over a sleeping dog"
190# # # # # # words = sentence.split()
191# # # # # # print(words)
192