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