-
Notifications
You must be signed in to change notification settings - Fork 567
Expand file tree
/
Copy pathstring-concatenation-indexint-slicing.py
More file actions
48 lines (39 loc) · 1.05 KB
/
string-concatenation-indexint-slicing.py
File metadata and controls
48 lines (39 loc) · 1.05 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
# Concatenation
string1 = "abra"
string2 = "cadabra"
magic_string = string1 + string2
print(magic_string)
first_name = "Arthur"
last_name = "Dent"
full_name = first_name + " " + last_name
print(full_name)
# Indexing
flavor = "fig pie"
print("Get character at index 1 of " + flavor )
print(flavor[1])
# get last character
print("Get last charakter")
print(flavor[-1])
# substring
flavor_substring = flavor[0:3]
print("Substring 0:3 of flavor")
print(flavor_substring)
# substring from 3 till end
flavor_end = flavor[3:]
print(flavor_end)
# no IndexError if I try to slice between boundaries outside the beginning or
# ending of a string
print("End of substring is outside the index range")
print(flavor[:14])
print("Start and End are outside the strings lenght")
print(flavor[13:15])
# strings are immutable meaning characters cannot be changed
# changes require new string assignments
word = "goal"
print("Initial word " + word)
word = "f" + word[1:]
print("Result after concatenation of f plus word[1:]")
print(word)
# exercise
test = "bazinga"
print(test[2:6])