-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStrings2.py
128 lines (63 loc) · 2.17 KB
/
Strings2.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Strings are one of pythons sequence data types
# 01234567890123
parrot = "Norwegian Blue"
print(parrot)
# Use brackets in an expression to print a specific character from a string
# Ex: parrot[3] will print the "w" character since the character count starts at 0
# with N in "Norwegian Blue" being 0, O being 1, R being 2 and W being 3
print(parrot[3])
# Mini Challenge:
# Add some code ot the program, so that it prints out "we win"
# Each character should appear on a separate line.
# The program should get the characters from the parrot string, using indexing.
# The w is already printed out, you just need to print the remaining 5 characters.
# With the text that is already being printed, the final output from the program should be:
# Norwegian Blue
# w
# e
#
# w
# i
# n
print(parrot[4])
print()
print(parrot[3])
print(parrot[6])
print(parrot[8])
# You can index backwards as well by using a "-" symbol
print()
print(parrot[-11])
print(parrot[-10])
print()
print(parrot[-11])
print(parrot[-8])
print(parrot[-6])
# Slicing
# The expression below is asking python to start at position 0 and slice all the way
# up to, but not including position 6
# Last character is not included in slice
print(parrot[0:6]) #Norweg
print(parrot[0:5]) #we
print(parrot[0:9]) #Norwegian
print(parrot[:9]) # Same as starting from 0
print(parrot[10:14])
#or
print(parrot[10:])
# Same rules apply for using a "-" sign as well
print(parrot[-4:-2])
print(parrot[-4:12])
# Using a step in a slice
print(parrot[0:6:2]) #Nre
#The slice start at index postion 0.
#it extends up to (but not including) position 6.
#We step through the sequence in steps of 2
print(parrot[0:6:3]) #Nw
# The slice starts at index position 0.
#it extends up to (but not inlcuding) position 6.
#We step through the sequence in steps of 3
number = "9,223,372,036,854,775,807"
print(number[1::4])
# Starts at position 1 and slices every 4th character
# Omitting the middle number allows the slice to go all the way through the variable
# Slicing backwards
input()